SQL AND OR query



我正在尝试从我们的客户服务查询中选择一些消息,其中 Mmessage 所有者是 ABC,数据像 ABCDEF 和消息......以及消息是从客户到 CSservice 或从 CSservice 到客户的。

我该怎么做?

SELECT Date, From, To, Data 
FROM Ccustomers WITH (NoLock)
WHERE MSGowner = 'ABC' AND Data LIKE '%ABCDEF%' AND 
([From] ='Customer' AND [To] = 'CSservice') OR ([From] ='CSservice' AND [To] = 'Customer') 
ORDER by Date
SELECT Date, From, To, Data 
FROM Ccustomers WITH (NoLock)
WHERE MSGowner = 'ABC' 
AND Data LIKE '%ABCDEF%' 
AND 
(
   ([From] = 'Customer'  AND [To] = 'CSservice') OR 
   ([From] = 'CSservice' AND [To] = 'Customer')
)
ORDER by Date

您的查询基本上是正确的。但是你必须考虑到and连接比or"更强"。要获得所需的输出,您需要设置括号。

试试这个:

SELECT Date, [From], [To], Data 
FROM Ccustomers WITH (NoLock)
WHERE MSGowner = 'ABC' 
AND Data LIKE '%ABCDEF%' 
AND (([From] = 'Customer'  AND [To] = 'CSservice') OR ([From] = 'CSservice' AND [To] = 'Customer'))
ORDER BY Date;

相关内容

  • 没有找到相关文章

最新更新