2016-10-01 104 views
0

請考慮以下模式: 供應商(sid:整數,sname:字符串,地址:字符串) ,零件(pid:整數,pname:字符串,顏色:字符串) 和目錄(sid:整數,pid:整數,成本:實數)如何刪除SQL中有條件編號的所有條目

目錄關係列出供應商對部件收取的價格。

查找僅供應紅色部件的供應商的標識。

因此,我知道我最終需要選擇供應商的目錄sid,但我不知道如何刪除供應商sid的所有實例,如果他們出售並且不是紅色的項目。

希望任何方向。我一直在嘗試做AND的組合,但似乎無法消除銷售不紅色部分的所有sid。

回答

1

供應商誰賣只有紅色部分:「誰找只提供紅零部件供應商的小島嶼發展中國家」

Select distinct c.Sid 
From Catalog c join Parts p 
    on p.pid = c.pid 
Where p.Color = 'RED' 
    and Not exists 
     (Select * from Catalog 
     Where sid = c.Sid 
      and pid in (Select Pid from Parts 
         Where Color != 'RED')) 

在英語中,它幾乎正是你的問題

聲明

其中,字only轉化爲它的兩個部分....相當於

「查找誰供應紅色部分和不提供不屬於任何紅色的零部件供應商的小島嶼發展中國家。」

左右....

Select distinct c.Sid -- "Find the sids of suppliers ..." 
From Catalog c join Parts p 
    on p.pid = c.pid 
Where p.Color = 'RED' -- "who supply red parts ... " 
    and Not exists  -- " and do not supply ..." 
     (Select * from Catalog -- " Parts ... " 
     Where sid = c.Sid  
      and pid in (Select Pid from Parts -- " ... That are ..." 
         Where Color != 'RED')) -- " ... Not Red" 
+0

你願意提供查詢的正常英文描述?我是SQL新手,它真的有幫助。 – user3577756

+0

當然,我會將其添加到上面的答案 –