简单的MySQL查询,ON JOINS

时间:2013-06-20 14:03:22

标签: mysql database join

我有两个表,Car表和Driver表。

在“驱动程序”表中,有namebirthDateid。 在Car表格中有regNomanuIDcolourpricedriverID

driverIDid 匹配,但有一个Driver.id不在Car.driverID

我怎样才能只显示Car表中没有的那个。

我试过了:

SELECT id FROM Driver JOIN Car ON Car.driverID!=Driver.id;

4 个答案:

答案 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了解其他方法。