2011-01-31 38 views
0

我似乎無法掌握滿足此問題需求的sql。 到目前爲止,我有:如何在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值來搜索反應,但遇到的問題IM是,如果我有:

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

然後它只返回的第一個結果真正的條件不是任何真實的條件。

如何返回符合此條件的行的所有結果?

+0

XOR是什麼意思,你的意思是參展商必須只與其中一個條件相符?注意:OR的前兩部分是相同的。 – RichardTheKiwi 2011-01-31 03:15:59

回答

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 

他們都將在輸出結果集

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 
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') 
相關問題