1

这是mysql查询

select distinct C.sid
 from Catalog C  
where not exists 
( select * from Parts P where P.pid = C.pid and P.color != 'red');

但它退回了供应红色零件的供应商,而不仅仅是红色零件?以及如何改变它的想法

我有 3 张桌子

Suppliers(sid: integer, sname: string, address: string) 
Parts(pid: integer, pname: string, color: string) 
Catalog(sid: integer, pid: integer, cost: real) 

任何帮助将不胜感激,谢谢

4

2 回答 2

1
select
    sid
from
    suppliers s
where
    exists (
        select 
            'x'
        from
            catalog c
                inner join
            parts p
                on c.pid = p.pid
        where
            s.sid = c.sid and
            p.color = 'red'
    ) and
    not exists (
        select
            'x'
        from
            catalog c
                inner join
            parts p
                on c.pid = p.pid
        where
            s.sid = c.sid and
            p.color != 'red'
    )
于 2013-11-12T23:54:39.897 回答
0

尝试

Select c.id
From catalog c
Where exists (
     Select 1 From Parts p
     Where p.pid = c.pid And p.color = 'red')
And not exists (
     Select 1 From Parts p
     Where p.pid = c.pid And p.color <> 'red')
于 2013-11-12T23:50:51.103 回答