我有2张桌子
tableCars
-------
car color
---------------
Mercedes blue
Mercedes red
Mercedes white
BMW blue
BMW red
BMW white
AUDI red
AUDI white
tableColors
-------
color
-------
blue
red
white
我需要在 tableCars 中找到 tableColors: AUDI 中并非所有颜色的汽车。
看起来并不难,但我尝试了左连接并且不存在变体,但我找不到解决方案。
最简单的方法是计数:
select ca.car
from Cars ca join
Colors co
on ca.color = co.color
group by ca.car
having count(*) <> (select count(*) from Colors);
这假设没有重复项(在任一表中)。如果您有重复项,请使用count(distinct color)
代替count(*)
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句