Search code examples
sqlqsqlquery

SQL AND OR query


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

Solution

  • 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