未知列where子句

时间:2015-12-15 01:58:53

标签: mysql sql doctrine-orm dql

我有以下DQL查询

SELECT
    ps.id,
    MAX(ps.dueDate) as due_date,
    u.firstName as first_name,
    u.lastName as last_name,
    u.email,
    IDENTITY(ps.loanApplication) as loan_application_id,
    DATE_DIFF(MAX(ps.dueDate), CURRENT_DATE()) as diff
FROM
    Loan\Entity\PaymentSchedule ps
LEFT JOIN
    ps.paymentType pt
LEFT JOIN
    ps.loanApplication la
LEFT JOIN
    la.status s
LEFT JOIN
    la.user u
WHERE
    pt.slug != :paymentSlug AND s.keyIdentifier = :status AND diff = 14
GROUP BY
    ps.loanApplication

转换为以下SQL查询

SELECT
    p0_.id AS id_0,
    MAX(p0_.due_date) AS sclr_1,
    u1_.first_name AS first_name_2,
    u1_.last_name AS last_name_3,
    u1_.email AS email_4,
    p0_.loan_application_id AS sclr_5,
    DATEDIFF(MAX(p0_.due_date), CURRENT_DATE) AS sclr_6 
FROM
    payment_schedule p0_
LEFT JOIN
    payment_type p2_ ON p0_.payment_type_id = p2_.id
LEFT JOIN
    loan_application l3_ ON p0_.loan_application_id = l3_.id
LEFT JOIN 
    loan_application_status l4_ ON l3_.loan_application_status_id = l4_.id
LEFT JOIN
    user u1_ ON l3_.user_id = u1_.id
WHERE
    p2_.slug <> ? AND l4_.key_identifier = ? AND sclr_6 = 14
GROUP BY
    p0_.loan_application_id

这给了我以下错误

======================================================================
 PDOException
 SQLSTATE[42S22]: Column not found: 1054 Unknown column 'sclr_6' in 'where clause'
----------------------------------------------------------------------

当我替换WHERE条件

WHERE pt.slug != :paymentSlug AND s.keyIdentifier = :status AND diff = 14

使用

WHERE pt.slug != :paymentSlug AND s.keyIdentifier = :status

它完美运行并显示正确的记录,我也尝试了WHERE条件

WHERE pt.slug != :paymentSlug AND s.keyIdentifier = :status AND DATE_DIFF(MAX(ps.dueDate), CURRENT_DATE()) = :days_diff

WHERE pt.slug != :paymentSlug AND s.keyIdentifier = :status HAVING (DATE_DIFF(MAX(ps.dueDate), CURRENT_DATE())) = :days_diff

以上WHERE不起作用,我在这里缺少什么?

感谢。

3 个答案:

答案 0 :(得分:1)

如果要在WHERE子句中使用别名,则需要sub-select

select *
from 
(SELECT
    p0_.id AS id_0,
    MAX(p0_.due_date) AS sclr_1,
    u1_.first_name AS first_name_2,
    u1_.last_name AS last_name_3,
    u1_.email AS email_4,
    p0_.loan_application_id AS sclr_5,
    DATEDIFF(MAX(p0_.due_date), CURRENT_DATE) AS sclr_6 
FROM
    payment_schedule p0_
LEFT JOIN
    payment_type p2_ ON p0_.payment_type_id = p2_.id
LEFT JOIN
    loan_application l3_ ON p0_.loan_application_id = l3_.id
LEFT JOIN 
    loan_application_status l4_ ON l3_.loan_application_status_id = l4_.id
LEFT JOIN
    user u1_ ON l3_.user_id = u1_.id
) A
WHERE
    slug <> ? AND key_identifier = ? AND sclr_6 = 14

这是逻辑处理查询的方式

FROM clause
WHERE clause
SELECT clause
GROUP BY clause
HAVING clause
ORDER BY clause

由于Where出现在Select之前,您无法在alias name子句中使用Where

答案 1 :(得分:0)

您不能在WHERE子句中使用别名(在最终结果字段中);但是,至少对于MySQL,您可以使用HAVING子句而不需要GROUP BY

答案 2 :(得分:0)

您使用的表达式是聚合的结果。替换添加having子句,使查询看起来像;

SELECT . . .
WHERE p2_.slug <> ? AND l4_.key_identifier = ? 
GROUP BY p0_.loan_application_id
HAVING sclr_6 = 14

请注意date_diff()不是MySQL中的函数。你打算datediff()