2009-11-03 87 views
0

假設我有一個日誌誰在特定天數,如客戶有:SQL查詢 - 某種選擇不同?

Cs day 
-- --- 
01 Tue 
02 Tue 
03 Wed 
01 Wed 
04 Thu 
02 Thu 

我需要返回只有那些誰在無論在星期二和星期三客戶的#分別查詢在這種情況下,只有CS#01

回答

6
select distinct c1.Cs 
from Customers c1 
inner join Customers c2 on c2.Cs=c1.Cs 
where c2.day='Tue' and c1.day='Wed' 
+1

我扔了一個獨特的存在,以防萬一CS /天的條目不是唯一的。 – 2009-11-03 20:16:39

+0

非常好,謝謝! – Nonny 2009-11-03 21:57:51

0

而且使用子查詢...

Select distinct cs From Customers 
Where Exists (Select * from Customers 
       Where day = 'Tue') 
    And Exists (Select * from Customers 
       Where day = 'Wed') 
+1

您缺少一個與用戶ID相關的條件。您的查詢將返回表中的所有記錄。 – 2009-11-03 20:19:45