SQL AND OR query

ぐ巨炮叔叔 提交于 2019-12-12 04:54:56

问题


I am trying to select some messages from our customer service queries, WHERE Mmessage owner is ABC, data are LIKE ABCDEF AND message ... AND where the message is either from Customer to CSservice or from CSservice to Customer.

How can I do that?

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

回答1:


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



回答2:


Your query was basically correct. But you have to consider that the and connection is "stronger" than the or. To get to your desired output you need to set brackets.

Try this:

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;


来源:https://stackoverflow.com/questions/28168040/sql-and-or-query

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!