mysql - 预订 : I want to display all records with a condition including if the record is null in table number 2

标签 mysql sql

我正在开发一个预订系统,我将向用户显示所有状态为 Verified 的记录,carType 与用户想要的相同,以及与用户输入的服务类型相同的记录。问题是,如果汽车记录在预订表中没有现有记录,则记录不会显示。重叠日期的检查器有效,但它不显示已验证的其他记录、相同的服务类型、相同的车型但在预订表中为空。

    $startDate = '2019-06-04 07:00:00';
    $endDate = '2019-06-20 07:00:00';
    $serviceType = "Self-drive";
    $carType = "SUV";


    $sql = "SELECT carrecord.recordID, carrecord.recordLong, carrecord.recordLat, 
model.brandName, model.modelName, carrecord.amount, model.carType, carrecord.status, 
carrecord.recordPicture, booking.recordID AS bookRecordID FROM carrecord
    LEFT JOIN booking
    ON booking.recordID = carrecord.recordID
    LEFT JOIN car
    ON car.carID  =  carrecord.carID
    LEFT JOIN model
    ON model.modelID = car.modelID  
    WHERE
    model.carType = '$carType'
    AND
    carrecord.serviceType = '$serviceType'
    AND
    carrecord.status = 'Verified'
    OR booking.recordID IS NULL
    AND NOT EXISTS
    (
        SELECT booking.recordID FROM booking 
        WHERE '$startDate' <= booking.endDate
        AND '$endDate' >= booking.startDate
        AND booking.status = 'Accepted'
    )";

最佳答案

更改 booking.recordID IS NULL,model.carType = '$carType' 条件并将它们移到 on 子句中

SELECT carrecord.recordID, carrecord.recordLong, carrecord.recordLat, 
model.brandName, model.modelName, carrecord.amount, model.carType, carrecord.status, 
carrecord.recordPicture, booking.recordID AS bookRecordID
    FROM carrecord
    LEFT JOIN booking
    ON booking.recordID = carrecord.recordID           
    LEFT JOIN car
    ON car.carID  =  carrecord.carID
    LEFT JOIN model
    ON model.modelID = car.modelID  and model.carType = '$carType'
    WHERE   carrecord.serviceType = '$serviceType'
    AND
   carrecord.status = 'Verified'     

     NOT EXISTS
    (
        SELECT booking.recordID FROM booking 
        WHERE '$startDate' <= booking.endDate
        AND '$endDate' >= booking.startDate
        AND booking.status = 'Accepted'
    )

关于mysql - 预订 : I want to display all records with a condition including if the record is null in table number 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56680741/

相关文章:

php - 使用 PHP 将今天日期的数据输出到 JSON

Mysql加入创建 View 命令

mysql - 如何使用具有最大投票值的 count() 检索图像路径值

sql主键和索引

mysql - MySQL 中何时使用单引号、双引号和反引号

javascript - 如何将 php、sql 和 Javascript 连接在一起,

mysql - 使用 mysql 选择前 N *组*

mysql - 数据从mysql导入到solr?

python - 在 Django 中使用 sql concat() 连接两列

sql - 在 DACPAC 中使用 SQLCMD 变量作为用户名