【发布时间】:2014-01-14 12:43:29
【问题描述】:
您好,我有一个包含 5 个表的数据库: 用户, 驱动器, 客户, 司机, 车辆。
我正在尝试获取所有驱动器及其相应的车辆驱动程序和客户端。 我想出了以下查询:
SELECT drives.id,
drives.driver AS driver_id,
CONCAT(LEFT(drivers.name, 1), '. ', drivers.surname) AS driver_name,
drives.client AS client_id,
CONCAT(LEFT(clients.name, 1), '. ', clients.surname) AS client_name,
drives.vehicle AS vehicle_id,
vehicles.license_plates AS license_plates,
drives.departure,
drives.destination,
drives.distance,
drives.type,
drives.payment_type,
drives.timestamp,
drives.total,
drives.expenses,
drives.profit,
CASE
WHEN DATE(drives.timestamp) < DATE(NOW()) AND drives.total > 0 THEN 'Completed'
WHEN DATE(drives.timestamp) < DATE(NOW()) AND drives.total = 0 THEN 'Overdue'
WHEN DATE(drives.timestamp) >= DATE(NOW()) THEN
CASE
WHEN drives.total = 0 THEN 'Pending'
WHEN drives.total > 0 THEN 'Prepaid'
END
END AS payment_status,
DATE_FORMAT(drives.timestamp, '%d-%m-%Y %H:%i:%s') AS 'stamp'
FROM drives, clients, drivers, vehicles WHERE
drives.driver = drivers.id AND
drives.client = clients.id AND
drives.vehicle = vehicles.id AND
drives.user = '146' ORDER BY id ASC LIMIT 9999999999 OFFSET 0
一切正常,但是如果我从车辆表中删除车辆记录,然后尝试从驱动器表中获取所有驾驶记录,则具有 drive.vehicle = vehicle.id (不再存在)的驾驶记录将不会打印出来。
如您所知,这不是我想要的。即使车辆、客户、司机被删除,我也希望打印所有的驾驶记录。
驱动器表数据示例:
id timestamp user driver client vehicle departure destination distance type payment_type total expenses profit note
1 2013-02-14 10:33:26 146 1 1 1 Address 1 Address 2 0 Deprature Cash 0 0 0 hello world
【问题讨论】: