Sql 在另一个表中查找丢失的记录
我有两张桌子Sql 在另一个表中查找丢失的记录,sql,Sql,我有两张桌子 tableCars ------- car color --------------- Mercedes blue Mercedes red Mercedes white BMW blue BMW red BMW white AUDI red AUDI white tableColors ------- color ------- blue red white 我需要在TableColor中找到没有所有颜
tableCars
-------
car color
---------------
Mercedes blue
Mercedes red
Mercedes white
BMW blue
BMW red
BMW white
AUDI red
AUDI white
tableColors
-------
color
-------
blue
red
white
我需要在TableColor中找到没有所有颜色的汽车TableColor:奥迪
看起来没那么难,但我尝试了左连接,但不存在差异,但我找不到解决方案。最简单的方法是计数:
select ca.car
from Cars ca join
Colors co
on ca.color = co.color
group by ca.car
having count(*) <> (select count(*) from Colors);
这假设两个表中都没有重复项。如果有重复项,则使用countdistinct color而不是count*只需分组即可。使用having子句验证汽车的颜色少于所有颜色
select car
from cars
group by car
having count(*) < (select count(*) from colors);
假设除tablecolor中的颜色外,没有其他颜色:
如果car/color可能有重复项,请切换到countdistinct color而不是count*SQL的技术类型是什么?Oracle、MSSQL、MYSQL等?也展示一些您尝试过的解决方案。我不理解这个问题。到目前为止您尝试了什么?
select car
from Cars
group by ca.car
having count(*) <> (select count(*) from Colors);