1

下面的查询会加载所有对话,但是,我只想加载两个人之间的对话。

SELECT * FROM msg 
WHERE senderid = '{$sender}' 
OR recid = '{$recipient}' 
OR recid = '{$sender}' 
OR senderid = '{$recipient}' 
order by id asc

我将如何一起使用 AND & OR 运算符?下面的工作吗?

SELECT * FROM msg 
WHERE senderid = '{$sender}' 
AND recid = '{$recipient}' 
OR recid = '{$sender}' 
AND senderid = '{$recipient}' 
order by id asc
4

2 回答 2

4

一起使用ANDandOR时,请使用括号对您的逻辑进行分组,并确保它符合您的预期:

SELECT * FROM msg 
WHERE (senderid = '{$sender}' AND recid = '{$recipient}')
OR    (recid = '{$sender}' AND senderid = '{$recipient}')
order by id asc
于 2012-08-21T14:40:59.153 回答
0

如下请见。

SELECT * FROM msg 
WHERE (senderid = '{$sender}' AND recid = '{$recipient}') 
OR (recid = '{$sender}' AND senderid = '{$recipient}') 
order by id asc
于 2012-08-21T14:44:19.057 回答