2011-02-04 10 views

答えて

1

今テーブル構造

Fruit | Attribute 
Apple A 
Apple B 
Banana A 
Banana B 
Orange A 

クエリ

select t1.* from tbl t1 
where t1.attribute = 'A' 
and not exists (select * from tbl t2 
       where t2.fruit=t1.fruit and t2.attribute <> 'A') 
+0

<>そのコマンドは何のために使われていますか? –

+0

"does not equal" – RichardTheKiwi

0

単に 'SQL' タグ、これは標準SQL-2003です:

WITH tbl (Fruit, Attribute) 
    AS 
    (
     SELECT Fruit, Attribute 
     FROM (
       VALUES ('Apple', 'A'), 
        ('Apple', 'B'), 
        ('Banana', 'A'), 
        ('Banana', 'B'), 
        ('Orange', 'A') 
      ) AS tbl (Fruit, Attribute) 
    ) 
SELECT T1.Fruit, T1.Attribute 
    FROM tbl AS T1 
WHERE T1.Attribute = 'A' 
     AND 1 = (
       SELECT COUNT(*) 
        FROM tbl AS T2 
       WHERE T2.Fruit = T1.Fruit 
       ); 
関連する問題