【问题标题】:Doctrine 2 DQL SubqueryDoctrine 2 DQL 子查询
【发布时间】:2013-08-29 08:30:59
【问题描述】:

我正在尝试通过 Doctrine 2.3 DQL 在 FROM 块中使用子查询。

每次我运行这个,都会出现以下错误

[Semantical Error] line 0, col 839 near '(SELECT ': Error: Class '(' is not defined.

这是我当前的 DQL 代码

$query = $this->getEntityManager()->createQuery('
SELECT 
        taS2.netDueDate,
        SUM(taS2.amountInThousands)                     AS SumAmountInThousands,
        SUM(taS2.netDAO1)                                       AS SumnetDAO1,
        SUM(taS2.netDAO2)                                               AS SumnetDAO2,
        SUM(taS2.netDAO3)                                           AS SumnetDAO3,
        SUM(taS2.netDAO4)                                               AS SumnetDAO4,
        SUM(taS2.netDueDate * taS2.amountInThousands)   AS SumNetDueDateInThousands,
        SUM(taS2.critical)  
FROM 
    (SELECT 
        ta.id,
        ta.documentDate,
        SUBSTRING(CONCAT(ta.netDueDate, \'\'), 9, 2) AS netDueDate,
        ta.discountRate, 
        ta.amountInThousands, 
        ta.netDAO1, 
        ta.netDAO2, 
        ta.netDAO3, 
        ta.netDAO4,
        ta.netDueDate,
        1 AS counter,
        CASE 
             WHEN (ta.clearingDate > ta.netDueDate) AND 
                     SUBSTRING(CONCAT(ta.clearingDate, \'\'), 6, 2) <> SUBSTRING(CONCAT(ta.netDueDate, \'\'), 6, 2)
             THEN ta.amountInThousands ELSE 0
        END AS critical
    FROM 
        AcmeBundle:Transaction ta
    WHERE
        ta.discountRate = 0) taS2

GROUP BY taS2.netDueDate
ORDER BY SUM(taS2.amountInThousands) DESC;
);

有人知道我如何正确地做到这一点吗?

【问题讨论】:

    标签: symfony doctrine-orm doctrine dql


    【解决方案1】:

    您的查询看起来更像是带有一些DQL 功能的普通SQL。您可以使用$this-&gt;getEntityManager()-&gt;createNativeQuery(); 执行 SQL 查询

    另请阅读Doctrine2 Subqueries

    【讨论】:

    • 感谢您的回复。此链接转到教义 1 文档,我使用的是教义 2.3。
    • 你的权利,对不起。如果可能,您仍应使用 native SQL 查询。
    猜你喜欢
    • 1970-01-01
    • 2021-08-05
    • 2012-11-15
    • 2013-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-02
    • 1970-01-01
    相关资源
    最近更新 更多