SQL AND OR 查询
SQL AND OR query
我正在尝试 select 来自我们客户服务查询的一些消息,其中 Mmessage 所有者是 ABC,数据是 LIKE ABCDEF AND message ...并且消息是从 Customer 到 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
连接 "stronger"。要获得所需的输出,您需要设置括号。
试试这个:
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 来自我们客户服务查询的一些消息,其中 Mmessage 所有者是 ABC,数据是 LIKE ABCDEF AND message ...并且消息是从 Customer 到 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
连接 "stronger"。要获得所需的输出,您需要设置括号。
试试这个:
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;