mysql - 仅选择与 IN 子句下指定的所有值匹配的那些行

标签 mysql sql

我有这张表:

CAR (model, color, engine) #PrimaryKey(model,engine)

(1081, 'blue', 'ec41')
(1082, 'cyan', 'ec41')
(1083, 'rose', 'ec41')

(1081, 'green', 'dc41')
(1082, 'white', 'dc41')

对于这组模型(1081,1082,1083)输出应该包含颜色 -(蓝色,青色,玫瑰色),而绿色和白色应该被忽略。(因为引擎(dc41)中没有成员(1083) ).
我试过这个:

select color from car where model in (1081,1082,1083)

但是,上面也输出绿色和白色。我如何限制它 - 仅从提供所有模型的引擎返回颜色。

最佳答案

有一个子查询返回具有所有 3 个模型 (1081,1082,1083) 的引擎。

select color
from car
where engine in (
    select engine
    from car
    where model in (1081,1082,1083)
    group by engine
    having count(distinct model) = 3)

关于mysql - 仅选择与 IN 子句下指定的所有值匹配的那些行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58834312/

相关文章:

PHP根据msg_id显示数据

具有顺序和限制的MySQL查询

mysql - 如何从2个表中获取所有数据

sql - Oracle SQL 通过 sibling 识别 sibling 并计算相关比率

MySQL查询: sum column values with distinct for another column

MYSQL查找n天内没有更新日志条目的帐户

php - 在Android中通过文件(使用FileInputStream)将图像发送到PHP并通过Blob保存mysql

sql - 从 Oracle 中的逗号分隔变量中删除重复值

sql - Oracle 11g XE "alter table drop column"导致 ORA-00600

sql - 在 SQL SELECT 语句中重用别名字段