2

我有以下查询,我想将其转换为使用教义的查询生成器。

SELECT
  u.user_id,
  u.username,
  u.create_date AS join_date,
  u.last_login_date,
  u.membership_level,
  u.create_date,
  avg(round((ug.toggle_count / ceil((g.ply_count + 1) / 2)) * 100, 1)) AS __avg_toggle_ratio,
  count(g.game_id) AS __game_count,
  ugse.rating AS __echess_rating,
  ugse.total_win_count AS __echess_win_count,
  ugse.total_loss_count AS __echess_loss_count,
  ugse.total_draw_count AS __echess_draw_count,
  (SELECT concat(ugsl.rating,'|',ugsl.total_win_count,'|',ugsl.total_loss_count,'|',ugsl.total_draw_count) FROM user_game_stats_live ugsl WHERE ugsl.user_id = u.user_id AND ugsl.game_time_class = 'lightning') AS __lightning_data,
  (SELECT concat(ugsl.rating,'|',ugsl.total_win_count,'|',ugsl.total_loss_count,'|',ugsl.total_draw_count) FROM user_game_stats_live ugsl WHERE ugsl.user_id = u.user_id AND ugsl.game_time_class = 'blitz') AS __blitz_data,
  (SELECT concat(ugsl.rating,'|',ugsl.total_win_count,'|',ugsl.total_loss_count,'|',ugsl.total_draw_count) FROM user_game_stats_live ugsl WHERE ugsl.user_id = u.user_id AND ugsl.game_time_class = 'standard') AS __standard_data,
  (SELECT uts.rating FROM user_tactics_settings uts WHERE uts.user_id = u.user_id AND uts.attempt_count >= 10) AS __tactics_rating
FROM
  game g
JOIN user_game ug ON g.game_id = ug.game_id
JOIN user_game_stats_email ugse ON ug.user_id = ugse.user_id
JOIN user u ON ug.user_id = u.user_id
WHERE
  g.last_move_time >=  DATE_SUB(NOW(), INTERVAL 5 DAY) AND
  g.ply_count >= 20 AND
  u.is_enabled = 1
GROUP BY
  ug.user_id
HAVING
  __avg_toggle_ratio >= 90 AND
  __game_count >= 10
ORDER BY
  __avg_toggle_ratio DESC

查询生成器是否能够重写这种查询?我应该改用其他方法吗,如果可以,怎么做?

4

1 回答 1

0

我不确定查询生成器,但我会使用“createNativeQuery”方法:

<?php
use Doctrine\ORM\Query\ResultSetMapping;

$rsm = new ResultSetMapping();
// build rsm here

$query = $entityManager->createNativeQuery("SELECT
                                                   u.user_id,
                                                   u.username,
                                                   u.create_date AS join_date,
                                                   u.last_login_date,
                                                   u.membership_level,
                                                   u.create_date,
                                                   avg(round((ug.toggle_count / ceil((g.ply_count + 1) / 2)) * 100, 1)) AS __avg_toggle_ratio,
                                                   count(g.game_id) AS __game_count,
                                                   ugse.rating AS __echess_rating,
                                                   ugse.total_win_count AS __echess_win_count,
                                                   ugse.total_loss_count AS __echess_loss_count,
                                                   ugse.total_draw_count AS __echess_draw_count,
                                                   (SELECT concat(ugsl.rating,'|',ugsl.total_win_count,'|',ugsl.total_loss_count,'|',ugsl.total_draw_count) FROM user_game_stats_live ugsl WHERE ugsl.user_id = u.user_id AND ugsl.game_time_class = 'lightning') AS __lightning_data,
                                                   (SELECT concat(ugsl.rating,'|',ugsl.total_win_count,'|',ugsl.total_loss_count,'|',ugsl.total_draw_count) FROM user_game_stats_live ugsl WHERE ugsl.user_id = u.user_id AND ugsl.game_time_class = 'blitz') AS __blitz_data,
                                                   (SELECT concat(ugsl.rating,'|',ugsl.total_win_count,'|',ugsl.total_loss_count,'|',ugsl.total_draw_count) FROM user_game_stats_live ugsl WHERE ugsl.user_id = u.user_id AND ugsl.game_time_class = 'standard') AS __standard_data,
                                                   (SELECT uts.rating FROM user_tactics_settings uts WHERE uts.user_id = u.user_id AND uts.attempt_count >= 10) AS __tactics_rating
                                             FROM game g
                                             JOIN user_game ug ON g.game_id = ug.game_id
                                             JOIN user_game_stats_email ugse ON ug.user_id = ugse.user_id
                                             JOIN user u ON ug.user_id = u.user_id
                                        WHERE g.last_move_time >=  DATE_SUB(NOW(), INTERVAL 5 DAY) 
                                          AND g.ply_count >= 20 
                                          AND u.is_enabled = 1
                                     GROUP BY ug.user_id
                                       HAVING __avg_toggle_ratio >= 90 
                                          AND __game_count >= 10
                                     ORDER BY __avg_toggle_ratio DESC', $rsm);
$query->setParameter(1, 'romanb');

$data = $query->getResult();
于 2013-05-29T06:50:58.710 回答