为什么这个查询总是返回桌子已被预订?

问题描述 投票:0回答:1
drop procedure if exists AddValidBooking;
DELIMITER //
CREATE PROCEDURE AddValidBooking(IN booking_date DATE, IN table_number INT)
BEGIN
    DECLARE booked BOOLEAN;

    START TRANSACTION;
    
    SELECT CASE WHEN EXISTS (
        SELECT 1
        FROM Bookings
        WHERE DATE(Booking_Date) = DATE(booking_date) AND Table_Number = table_number
    ) THEN TRUE ELSE FALSE END INTO booked;


    IF booked THEN
        ROLLBACK;
        SELECT CONCAT("table ", table_number, " is booked - booking canceled") AS booking_status;
    ELSE
        SELECT CONCAT("table ", table_number, " is free - booked successfully") AS booking_status;
        INSERT INTO Bookings (Booking_Date, Table_Number) VALUES (booking_date, table_number);
        COMMIT;
    END IF;
END //
DELIMITER ;


CALL AddValidBooking('2023-11-7', 2);

虽然免费,但返回表 2 总是被预订 我尝试了多种日期与表号的组合,但它仍然返回相同的结果

mysql database database-design
1个回答
0
投票

您需要将查询更改为:

WHERE DATE(Bookings.Booking_Date) = DATE(booking_date) AND Bookings.Table_Number = table_number

或者更好,更改参数名称。

© www.soinside.com 2019 - 2024. All rights reserved.