问题
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