2014-10-21 45 views
1
Suppliers(sid: integer, sname: string, address: string) 
Parts(pid: integer, pname: string, color: string) 
Catalog(sid: integer, pid: integer, cost: real) 

找到供應商每個零件的供應商名稱?之間的差異不存在,除了在sql中

select s.sname 
from suppliers s 
where NOT EXISTS (select p.pid 
        from parts p 
        where p.color='red' and 
         NOT EXISTS (select c.pid from parts p where c.pid=p.pid and c.sid=s.sid and p.color='red' 
            ) 
       ) 

此回答的內容可能不正確?

+2

你有預期的結果中的所有行?有錯誤嗎? – 2014-10-22 00:05:55

回答

1

像下面這樣的東西可能不那麼令人困惑。

Select s.name 
from suppliers s 
join catalog c on c.sid = s.sid 
join parts p on p.pid = c.pid 
WHERE Not Exists (Select p2.pid from parts p2 where p2.pid = c.pid) 
1

不存在和不存在條件函數。

如果內部子查詢未返回該特定記錄的任何行,則NOT EXISTS將打印特定的記錄或屬性值。

例如:

select c.sid 
from catalog c 
where NOT EXISTS (select * 
        from parts p 
        where p.color<>'red' and 
         p.pid=c.pid); 

這個查詢將返回所有誰只供應紅色部分的SID。


鑑於EXCEPT表現爲與我們在集合論中研究過的相同的差異算子。

例如:

A-B返回其不存在於B.