2011-02-04 3 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 
       );