0

I have two tables, Car table and Driver table.

In Driver table there are name, birthDate, and id. In Car table there are regNo, manuID, colour, price, and driverID.

THE driverID and id match, but there is one Driver.id which is not in Car.driverID

How will I be able to display only the one which is not in Car table.

I tried this:

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

4 回答 4

3

您可以使用子查询。

SELECT id FROM Driver WHERE id not in (select distinct driverid from car)
于 2013-06-20T14:05:27.917 回答
3

你可以这样做

SELECT * FROM `driver` WHERE `id` not in(select `driverId` from `car`)
于 2013-06-20T14:14:30.860 回答
1

如果你喜欢使用加入,那么你可以这样做:

SELECT d.id
FROM Driver d
LEFT JOIN Car c ON (d.id=c.driverID)
WHERE c.driverID is NULL;

您还可以查看此答案以了解其他方法。

于 2013-06-20T15:07:27.603 回答
1

尝试

SELECT c.*
FROM Cars AS C
LEFT JOIN Drivers AS D ON d.id=c.driverId
WHERE d.id IS NULL
于 2013-06-20T14:05:54.763 回答