我有两个表,Car
表和Driver
表。
在“驱动程序”表中,有name
,birthDate
和id
。
在Car表格中有regNo
,manuID
,colour
,price
和driverID
。
driverID
和id
匹配,但有一个Driver.id
不在Car.driverID
我怎样才能只显示Car表中没有的那个。
我试过了:
SELECT id FROM Driver JOIN Car ON Car.driverID!=Driver.id;
答案 0 :(得分:3)
您可以使用子查询。
SELECT id FROM Driver WHERE id not in (select distinct driverid from car)
答案 1 :(得分:3)
你可以这样做
SELECT * FROM `driver` WHERE `id` not in(select `driverId` from `car`)
答案 2 :(得分:1)
尝试
SELECT c.*
FROM Cars AS C
LEFT JOIN Drivers AS D ON d.id=c.driverId
WHERE d.id IS NULL
答案 3 :(得分:1)
如果你想使用join,那么你可以这样做:
SELECT d.id
FROM Driver d
LEFT JOIN Car c ON (d.id=c.driverID)
WHERE c.driverID is NULL;
您还可以查看this answer了解其他方法。