Where子句使用带有IF和IFNULL的别名列

时间:2017-10-30 01:31:58

标签: mysql subquery where alias ifnull

我有一个如下所示的mysql查询

SELECT 
        sppt_ticket.*, 
        IF(sppt_read_support_ticket.ID_aks_user IS NULL,'N', 'Y') AS `read_status`, 
        IFNULL(readcomment.total_comment, 0) AS unread_comment
    FROM 
        sppt_ticket
    LEFT JOIN 
        sppt_read_support_ticket ON 
        sppt_ticket.ID_support_ticket = sppt_read_support_ticket.ID_support_ticket AND 
        ID_aks_user = 1
    LEFT JOIN
        (SELECT 
            sppt_comment.ID_support_ticket, SUM(IF(sppt_read_comment.ID_aks_user IS NULL, 1, 0)) 
    AS 
        total_comment
    FROM 
        sppt_comment
    LEFT JOIN 
        sppt_read_comment 
    ON 
        sppt_comment.ID_comment = sppt_read_comment.ID_comment 
    AND 
        sppt_read_comment.ID_aks_user = 1
    GROUP BY 
        sppt_comment.ID_support_ticket) AS readcomment ON readcomment.ID_support_ticket = sppt_ticket.ID_support_ticket

我想在where子句中得到的内容是这样的

WHERE read_status = 'Y'

我尝试使用子查询,但我仍然没有得到它.. 任何帮助?谢谢你的建议

1 个答案:

答案 0 :(得分:0)

你试过这个:

SELECT * FROM
(
-- your original query as a table
SELECT 
    sppt_ticket.*, 
    IF(sppt_read_support_ticket.ID_aks_user IS NULL,'N', 'Y') AS `read_status`, 
    IFNULL(readcomment.total_comment, 0) AS unread_comment
FROM 
    sppt_ticket
LEFT JOIN 
    sppt_read_support_ticket ON 
    sppt_ticket.ID_support_ticket = sppt_read_support_ticket.ID_support_ticket AND 
    ID_aks_user = 1
LEFT JOIN
    (SELECT 
        sppt_comment.ID_support_ticket, SUM(IF(sppt_read_comment.ID_aks_user IS NULL, 1, 0)) 
AS 
    total_comment
FROM 
    sppt_comment
LEFT JOIN 
    sppt_read_comment 
ON 
    sppt_comment.ID_comment = sppt_read_comment.ID_comment 
AND 
    sppt_read_comment.ID_aks_user = 1
GROUP BY 
    sppt_comment.ID_support_ticket) AS readcomment ON readcomment.ID_support_ticket = sppt_ticket.ID_support_ticket
)
as temptable
where read_status = 'Y' -- this should work

如果您不想将查询视为表格,则可以使用HAVING代替WHERE

SELECT 
    sppt_ticket.*, 
    IF(sppt_read_support_ticket.ID_aks_user IS NULL,'N', 'Y') AS `read_status`, 
    IFNULL(readcomment.total_comment, 0) AS unread_comment
FROM 
    sppt_ticket
LEFT JOIN 
    sppt_read_support_ticket ON 
    sppt_ticket.ID_support_ticket = sppt_read_support_ticket.ID_support_ticket AND 
    ID_aks_user = 1
LEFT JOIN
    (SELECT 
        sppt_comment.ID_support_ticket, SUM(IF(sppt_read_comment.ID_aks_user IS NULL, 1, 0)) 
AS 
    total_comment
FROM 
    sppt_comment
LEFT JOIN 
    sppt_read_comment 
ON 
    sppt_comment.ID_comment = sppt_read_comment.ID_comment 
AND 
    sppt_read_comment.ID_aks_user = 1
GROUP BY 
    sppt_comment.ID_support_ticket) AS readcomment ON readcomment.ID_support_ticket = sppt_ticket.ID_support_ticket
HAVING read_status = 'Y' -- use HAVING instead of WHERE

将原始查询视为表的原因是因为在查询中评估值的方式。在原始查询中,别名read_status不能与WHERE子句一起使用,因为在评估WHERE子句时可能尚不知道实际值。如Section B.1.5.4, “Problems with Column Aliases”.中所述,将其视为表格可确保read_status的值已经过评估。

对于HAVING方法,MySQL创建了permits references in the HAVING clause to aliased expressions in the select list标准SQL的扩展。