mysql - 我正在尝试执行 mysql 查询以获取只供应红色部分的供应商?

标签 mysql sql database relational-database

这里是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张 table

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

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

最佳答案

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'
    )

关于mysql - 我正在尝试执行 mysql 查询以获取只供应红色部分的供应商?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19942240/

相关文章:

php - 将 "password is ok"存储在 php session 变量中?

mysql - 将纬度/经度字段转换为地理空间点

php - Mysql SUM() 与 PHP 普通加法运算

python - 无法比较原始偏移和偏移感知日期时间 - last_seen 选项

sql - PostgreSQL 交叉表函数

sql - CASCADE 和 RESTRICT 外键要求是否存在冲突?

database - 将列表列表保存到文件的最有效方法?

php - 去掉()前后的 ' '

mysql - 使用 MySQL 聚合重叠事件

phpMyAdmin/MySQL 导出用户/权限供以后导入