如何在SQL中使用类似XOR的函数

时间:2011-01-31 03:03:47

标签: sql sql-server database tsql

我似乎无法掌握满足我对这个问题的需求所需的sql。 我到目前为止:

select * from customformresponses
INNER JOIN exhibitors ON
Exhibitors.ExhibitorId= customformresponses.ExhibitorId
WHERE customformresponses.exhibitorid='8179cde9-b922-430a-9024-bd4cb8b3d05c'
  and exhibitors.exhibitionID = 'e641a3d4-cb57-4f67-86a1-5c2f4c3cf6e0'
  and customformresponses.FormID = 'c7f5f0de-35f8-412d-9c91-eaf8bb1a3c26' 

我需要在3个不同的formID值中搜索响应,但我遇到的问题是:如果我有:

customformresponses.FormID = 'c7f5f0de-35f8-412d-9c91-eaf8bb1a3c26'  or
customformresponses.FormID = 'c7f5f0de-35f8-412d-9c91-eaf8bb1a3c26' or
customformresponses.FormID = 'e69cee39-2519-434d-be3e-516ba156b444'

然后它只返回第一个真实条件的结果,而不是任何真实条件。

如何从符合此条件的行返回所有结果?

3 个答案:

答案 0 :(得分:1)

我认为这就是你的目标

select *
from customformresponses
INNER JOIN exhibitors
    ON Exhibitors.ExhibitorId= customformresponses.ExhibitorId
WHERE customformresponses.exhibitorid='8179cde9-b922-430a-9024-bd4cb8b3d05c'
AND exhibitors.exhibitionID = 'e641a3d4-cb57-4f67-86a1-5c2f4c3cf6e0'
AND
( customformresponses.FormID = 'c7f5f0de-35f8-412d-9c91-eaf8bb1a3c26'
  OR
  customformresponses.FormID = 'e69cee39-2519-434d-be3e-516ba156b444')

它需要匹配的Exhibitorid和exhibitionid,但对于FormID,它将匹配任何两个。

所以,如果你有两行

exhibitorid      | exhibitionid     | formid
8179cde9-b922... | c7f5f0de-35f8... | c7f5f0de-35f8-412d-9c91-eaf8bb1a3c26
8179cde9-b922... | c7f5f0de-35f8... | e69cee39-2519-434d-be3e-516ba156b444

它们都将在输出结果集

答案 1 :(得分:0)

如果只是为了避免重复GUID和列名......

SELECT *
FROM customformresponses
  INNER JOIN exhibitors ON Exhibitors.ExhibitorId = customformresponses.ExhibitorId
WHERE (
  CASE customformresponses.exhibitorid WHEN '8179cde9-b922-430a-9024-bd4cb8b3d05c' THEN 1 ELSE 0 END +
  CASE exhibitors.exhibitionID WHEN 'e641a3d4-cb57-4f67-86a1-5c2f4c3cf6e0' THEN 1 ELSE 0 END +
  CASE customformresponses.FormID WHEN 'c7f5f0de-35f8-412d-9c91-eaf8bb1a3c26' THEN 1 ELSE 0 END
) = 1

答案 2 :(得分:0)

select * from customformresponses 
INNER JOIN exhibitors     
ON Exhibitors.ExhibitorId= customformresponses.ExhibitorId 
WHERE customformresponses.exhibitorid='8179cde9-b922-430a-9024-bd4cb8b3d05c' 
AND exhibitors.exhibitionID = 'e641a3d4-cb57-4f67-86a1-5c2f4c3cf6e0' 
AND ormresponses.FormID IN  ( 'c7f5f0de-35f8-412d-9c91-eaf8bb1a3c26', 'e69cee39-2519-434d-be3e-516ba156b444')