带OR的MySQL查询只返回数据子集?

时间:2015-09-02 11:25:09

标签: mysql sql

我在db表上运行以下查询

SELECT *, CONVERT_TZ(`received_on`, '+00:00','+05:30') AS received_on 
FROM `user_chat_msgs` 
WHERE (`from_user`='54610' AND `to_user`='1') 
OR (`to_user`='1' AND `from_user`='54610') 
ORDER BY id DESC 
LIMIT 10000; 

这只返回

之类的消息

from_user = 54610和to_user = 1

预期结果

from_user = 54610和to_user = 1 from_user = 1和to_user = 54610

逻辑上查询听起来对我来说是正确的,我不确定这里有什么问题?

1 个答案:

答案 0 :(得分:2)

然后使用适当的where子句:

SELECT *, CONVERT_TZ(`received_on`, '+00:00','+05:30') AS received_on
FROM `user_chat_msgs`
WHERE (`from_user` = '54610' AND `to_user` = '1') OR
      (`from_user` = '1' AND `to_user` = '54610')
ORDER BY id DESC
LIMIT 10000; 

您的where条款:

WHERE (`from_user` = '54610' AND `to_user` = '1') OR
      (`to_user` = '1' AND `from_user` = '54610') 

只是重复这两个条件,但顺序不同。

注意:如果用户ID是一个数字,那么不要在常量周围使用引号。