让我们说,我有3张桌子,汽车,车主和车主。 现在我想要有完整的汽车清单,那些拥有车主和那些没有车的车。
当汽车拥有自己的车时,我必须拥有车主的数据。
Wrong query 1 (selects ownerless cars out):
SELECT * FROM car
LEFT JOIN cars_owner ON ...
INNER JOIN owner ON ...
Wrong query 2 (selects cars_owner relations without owners too):
SELECT * FROM car
LEFT JOIN cars_owner ON ...
LEFT JOIN owner ON ...
问题是:如何在mysql中加入一个带内连接的表?有什么想法吗?
答案 0 :(得分:0)
此查询返回拥有所有者的汽车:
SELECT * FROM car LEFT JOIN cars_owner ON (car.CarID=cars_owner.CarID) inner join owner ON (owner.OwnID = cars_owner.OwnID)
然而,这将使车辆没有车主:
SELECT * FROM car where (car.CarID!=(SELECT car.CarID FROM car LEFT JOIN cars_owner ON (car.CarID=cars_owner.CarID) inner join owner ON (owner.OwnID = cars_owner.OwnID)) )