2015-03-03 62 views
0

我有一個像下面SQL語句選擇的約束

table_fruit: 

account_id fruit 
1    apple 
1    banana 
2    apple 
2    orange 
2    banana 
3    apple 

一個表,我試圖讓每個人的名單有一個蘋果,並沒有其他的水果

這似乎是一個簡單的想法,但它遞給我現在...

回答

0

水果可以使用not exists作爲

select * from table_fruit t1 
where t1.fruit = 'apple' 
and not exists(
    select 1 from table_fruit t2 
    where t2.account_id = t1.account_id 
    and t2.fruit <> 'apple' 
); 
+0

非常感謝 – Dom 2015-03-03 15:12:21

0

試試這個:

Select * From table_fruit Where fruit = 'apple' 

或 SELECT * FROM table_fruit哪裏像「蘋果%%」

+0

這會抓住每個人有水果蘋果,我需要大家的是,只有一個蘋果。謝謝你嘗試。 – Dom 2015-03-03 15:08:38

+0

沒有得到正確的 – SoloMax 2015-03-03 19:39:36