我有这张表:
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/