【问题标题】:Mysql results from multiple tables where ids match don't appear多个表中没有出现 id 匹配的 Mysql 结果
【发布时间】: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

【问题讨论】:

    标签: mysql sql


    【解决方案1】:

    您当前正在使用 INNER JOIN,请考虑使用 OUTER JOIN

    ...
    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
    .... 
    

    【讨论】:

    • 请问上次加入时是否缺少“ON”?
    • 当然,一定有ON
    【解决方案2】:

    您应该在这里使用 JOINS,这肯定有助于捕获 NULL 值,LEFT JOIN 应该会有所帮助

    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
    

    【讨论】:

    • sql 给我以下错误:#1064 - 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 19 行的“LEFT JOIN Vehicles VI ON DRI.vehicle = vehicle.id WHERE drive.user = '146' ORD”附近使用正确的语法
    • 更新了查询,现在可以试试吗
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-27
    • 2019-06-19
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 2014-09-06
    相关资源
    最近更新 更多