Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
不显示ID匹配的多个表的Mysql结果_Mysql_Sql - Fatal编程技术网

不显示ID匹配的多个表的Mysql结果

不显示ID匹配的多个表的Mysql结果,mysql,sql,Mysql,Sql,您好,我有一个包含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), '.

您好,我有一个包含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

您当前使用内部联接,考虑使用外部连接< /p>
...
FROM drives 
LEFT OUTER JOIN clients ON drives.client = clients.id
LEFT OUTER JOIN drivers  ON drives.driver = drivers.id
LEFT OUTER JOIN vehicles ON drives.vehicle = vehicles.id
WHERE
.... 

您应该在这里使用连接,这肯定有助于捕获空值,左连接应该有帮助

SELECT DRI.id, DRI.driver AS driver_id, CONCAT(LEFT(DI.name, 1), '. ', DI.surname) AS driver_name,
DRI.client AS client_id, CONCAT(LEFT(CI.name, 1), '. ', CI.surname) AS client_name,
DRI.vehicle AS vehicle_id, vehicles.license_plates AS license_plates, DRI.departure,
DRI.destination, DRI.distance, DRI.type, DRI.payment_type, DRI.timestamp, DRI.total,
DRI.expenses, DRI.profit,
CASE
    WHEN DATE(DRI.timestamp) < DATE(NOW()) AND DRI.total > 0 THEN 'Completed'
    WHEN DATE(DRI.timestamp) < DATE(NOW()) AND DRI.total = 0 THEN 'Overdue'
    WHEN DATE(DRI.timestamp) >= DATE(NOW()) THEN
        CASE
            WHEN DRI.total = 0 THEN 'Pending'
            WHEN DRI.total > 0 THEN 'Prepaid'
        END     
END AS payment_status,
DATE_FORMAT(DRI.timestamp, '%d-%m-%Y %H:%i:%s') AS 'stamp'
FROM drives DRI
LEFT JOIN clients CI ON DRI.client = CI.id
LEFT JOIN drivers DI ON DRI.driver = DI.id,
LEFT JOIN vehicles VI ON DRI.vehicle = VI.id
WHERE
DRI.user = '146'
ORDER BY DRI.id ASC

我可以问一下上次加入时是否缺少ON?当然,肯定有ONsql给我以下错误:1064-您的SQL语法有错误;查看与您的MySQL服务器版本对应的手册,了解在DRI.vehicle=vehicles.id WHERE drives.user='146'ORD'第19行使用接近'LEFT JOIN vehicles VI ON drives.id'的正确语法更新了查询,您现在可以尝试吗
SELECT DRI.id, DRI.driver AS driver_id, CONCAT(LEFT(DI.name, 1), '. ', DI.surname) AS driver_name,
DRI.client AS client_id, CONCAT(LEFT(CI.name, 1), '. ', CI.surname) AS client_name,
DRI.vehicle AS vehicle_id, vehicles.license_plates AS license_plates, DRI.departure,
DRI.destination, DRI.distance, DRI.type, DRI.payment_type, DRI.timestamp, DRI.total,
DRI.expenses, DRI.profit,
CASE
    WHEN DATE(DRI.timestamp) < DATE(NOW()) AND DRI.total > 0 THEN 'Completed'
    WHEN DATE(DRI.timestamp) < DATE(NOW()) AND DRI.total = 0 THEN 'Overdue'
    WHEN DATE(DRI.timestamp) >= DATE(NOW()) THEN
        CASE
            WHEN DRI.total = 0 THEN 'Pending'
            WHEN DRI.total > 0 THEN 'Prepaid'
        END     
END AS payment_status,
DATE_FORMAT(DRI.timestamp, '%d-%m-%Y %H:%i:%s') AS 'stamp'
FROM drives DRI
LEFT JOIN clients CI ON DRI.client = CI.id
LEFT JOIN drivers DI ON DRI.driver = DI.id,
LEFT JOIN vehicles VI ON DRI.vehicle = VI.id
WHERE
DRI.user = '146'
ORDER BY DRI.id ASC