0

我写了这个查询,但它给了我一个错误:

select c.sid
from catalog c
where exists ((select p.color from parts p)
except (select p2.color from parts p2 where p2.color != 'Red'));

ORA-00907: 缺少右括号

你能告诉我哪里不正确吗?

目录:

   SID        PID       COST

     1          3         .5
     1          4         .5
     1          8       11.7
     2          3        .55
     2          8       7.95
     2          1       16.5
     3          8       12.5
     3          9          1
     4          5        2.2
     4          6 1247548.23
     4          7 1247548.23

部分:

   PID PNAME                               COLOR

     1 Left Handed Bacon Stretcher Cover   Red
     2 Smoke Shifter End                   Black
     3 Acme Widget Washer                  Red
     4 Acme Widget Washer                  Silver
     5 I Brake for Crop Circles Sticker    Translucent
     6 Anti-Gravity Turbine Generator      Cyan
     7 Anti-Gravity Turbine Generator      Magenta
     8 Fire Hydrant Cap                    Red
     9 7 Segment Display                   Green
4

1 回答 1

1

我想知道只供应红色零件的供应商的 sid

你可以写:

SELECT c.sid
  FROM catalog c
  JOIN parts p
    ON p.pid = c.pid
 WHERE p.color = 'Red'
MINUS
SELECT c.sid
  FROM catalog c
  JOIN parts p
    ON p.pid = c.pid
 WHERE p.color <> 'Red'
;

前面的零件MINUS找到所有供应红色零件的供应商;MINUS找到所有供应红色零件的供应商之后的零件;因此,整个查询会找到所有仅提供红色零件的不同供应商。

于 2012-10-05T19:36:45.423 回答