子查询中的Doctrine子查询

时间:2010-11-13 16:44:00

标签: mysql symfony1 doctrine

我正在尝试将原始mysql查询转换为使用doctrine。 该表中包含大量统计信息,我的查询正在检查每天平均增益偏离平均增长的平均值。

SQL版本与我期望它的行为完全一致。转换为Doctrine会给我一个错误。

这是原作:

SELECT
    l.*,
    DAY(l.created_at) as day,
    MONTH(l.created_at) as month,
    YEAR(l.created_at) as year,
    (
        MAX(l.infamyrenown) -
        MIN(l.infamyrenown) -
        (
            SELECT AVG(infamydifference) as avginf FROM
            (
                    SELECT (
                        MAX(inf.infamyrenown) -
                        MIN(inf.infamyrenown)
                        ) as infamydifference
                    FROM lotro_record inf
                    GROUP BY DAY(inf.created_at)
            ) as p1
        )
    ) as infamy_deviance
FROM
    lotro_record l
GROUP BY
    year,month,day

这是破坏的Doctrine查询:

Doctrine_Core::getTable("LotroRecord")
->createQuery("l")
->select("l.*")
->addSelect("DAY(created_at)")
->addSelect("MONTH(created_at)")
->addSelect("YEAR(created_at)")
->addSelect("(
    MAX(l.infamyrenown) -
    MIN(l.infamyrenown) -
    (
        select AVG(infamydifference) as avginf FROM (
            SELECT (
                MAX(inf.infamyrenown) -
                MIN(inf.infamyrenown)
            ) as infamydifference
            FROM LotroRecord inf
            GROUP BY DAY(inf.created_at)
        ) as p1
    )
) as infamy_deviance")
->where("lotro_character_id = {$this->getId()}")
->groupBy("DAY(created_at)");

哪个生成此SQL:

SELECT l.id                                        AS l__id, 
       l.infamyrenown                              AS l__infamyrenown, 
       l.kills                                     AS l__kills, 
       l.killing_blows                             AS l__killing_blows, 
       l.kills_above_rating                        AS l__kills_above_rating, 
       l.kills_below_rating                        AS l__kills_below_rating, 
       l.deaths                                    AS l__deaths, 
       l.lotro_character_id                        AS l__lotro_character_id, 
       l.created_at                                AS l__created_at, 
       l.updated_at                                AS l__updated_at, 
       DAY(l.created_at)                           AS l__0, 
       MONTH(l.created_at)                         AS l__1, 
       YEAR(l.created_at)                          AS l__2, 
       ( Max(l.infamyrenown) - Min(l.infamyrenown) - (SELECT 
         Avg(infamydifference) AS avginf 
                                                      FROM 
           (SELECT ( Max(l2.infamyrenown) - Min(l2.infamyrenown) ) AS l__0 
            FROM   lotro_record l2 
            GROUP  BY DAY(l2.created_at)) AS p1) ) AS l__3 
FROM   lotro_record l 
WHERE  ( l.lotro_character_id = 1 ) 
GROUP  BY DAY(l.created_at) 

错误是:

'字段列表'中的未知列'infamydifference'

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我认为您希望在您编写的DQL查询中将其称为inf.infamydifference,而不仅仅是infamydifference

select AVG(inf.infamydifference) as avginf