我在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
逻辑上查询听起来对我来说是正确的,我不确定这里有什么问题?
答案 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是一个数字,那么不要在常量周围使用引号。