如何从内连接表中丢弃值?

时间:2014-06-11 14:42:19

标签: mysql sql

我有以下SQL查询。

SELECT CON_ID, MAX(COM_DateTime) AS maxDate
FROM
(
    SELECT CON_ID, COM_DateTime
    FROM CON_Conversation
        INNER JOIN COU_ConversationUser AS cu USING (CON_ID)
        INNER JOIN COM_ConversationMessage AS cm USING (CON_ID)
    WHERE cu.USR_ID = 4328 AND cm.USR_ID = 4328 AND cu.COU_Status <> 'deleted'
 ) AS sub
 GROUP BY CON_ID
 ORDER BY maxDate DESC

从内连接COU_ConversationUser我想丢弃所有已删除状态的连接。这是我的表的样本。

COU_ConversationUser

如何使用相同的查询丢弃其表格中COU_Status等于删除的所有字段?在这种情况下只有1.但我还没有能够弄明白。

2 个答案:

答案 0 :(得分:1)

AND cu.COU_Status <> 'deleted'

应该已经有效了。由于它不起作用,您要过滤错误的字段,或者更有可能删除的结果与您要过滤的关键字不匹配。检查表格中的空格并尝试not like %deleted%。你似乎走在正确的轨道上,可能是一些小小的绊倒你。您也可以尝试:

and cu.COU_Status in ('read', 'unread')

答案 1 :(得分:0)

尝试将该字段添加到内部选择语句中,并过滤“已删除的”字样。记录。这应该接近你所需要的:

SELECT CON_ID, MAX(COM_DateTime) AS maxDate
FROM
(
    SELECT CON_ID, COM_DateTime, cu.COU_Status
    FROM CON_Conversation
        INNER JOIN COU_ConversationUser AS cu USING (CON_ID)
        INNER JOIN COM_ConversationMessage AS cm USING (CON_ID)
    WHERE cu.USR_ID = 4328 AND cm.USR_ID = 4328 AND cu.COU_Status <> 'deleted'
 ) AS sub
 WHERE sub.COU_Status <> 'deleted'
 GROUP BY CON_ID
 ORDER BY maxDate DESC