mysql的多个条件并不相等

时间:2015-10-29 18:22:31

标签: mysql

我想要这样的事情:

SELECT * FROM chat 
WHERE (user1= :user OR user2= :user) 
AND (deleted1 != :user OR deleted2 != :user) 
ORDER BY year, month, day, time DESC 
LIMIT 20

我有这段代码:

SELECT * FROM chat 
WHERE user1= :user OR user2= :user 
AND deleted1 <> :user OR deleted2<> :user 
ORDER BY year, month, day, time DESC 
LIMIT 20

我想首先检查:useruser1中是user2,然后检查:user是否不在deleted1deleted2 }。在许多情况下,deleted1deleted2都是空的。但是这个代码我所拥有的并不像我想要的那样。

enter image description here

这是我的表格,我希望在deleted1或deleted2中显示不是user1或user2的所有行

1 个答案:

答案 0 :(得分:2)

您可以尝试使用以下查询

SELECT *   FROM chat
WHERE      (user1= :user OR user2= :user)
AND        ( deleted1 <> :user )
AND        ( deleted2<> :user)
ORDER BY year, month, day, time DESC
LIMIT 20