我们可以在一个MySQL事件中编写多个查询吗?

gfttwv5a  于 2023-01-29  发布在  Mysql
关注(0)|答案(4)|浏览(168)

我想在Mysql事件的帮助下执行以下查询,但是当我在事件中添加delete语句并尝试创建它时,给了我Mysql错误。如果我选择跳过delete语句,则事件会毫无问题地创建。

INSERT INTO tbl_bookings_released
(
    id, row, seatnum, price,theatre_id, play_id, show_id, showtime, show_date, 
    isbooked, inserted_at, inserted_from, booking_num, tot_price, subzone_id, 
    zone_id, txn_id
)
SELECT 
    id, row, seatnum, price,theatre_id, play_id, show_id, showtime, 
    show_date, isbooked, inserted_at, inserted_from, booking_num, 
    tot_price, subzone_id, zone_id, txn_id
FROM tbl_bookings
WHERE (
    UNIX_TIMESTAMP( NOW( ) ) - UNIX_TIMESTAMP( inserted_at )
) /60 > 2
AND booking_num NOT
IN (
    SELECT booking_id
    FROM tbl_cust_booking
);

DELETE
FROM tbl_bookings
WHERE (
    UNIX_TIMESTAMP( NOW( ) ) - UNIX_TIMESTAMP( inserted_at )
) /60 > 2
AND booking_num NOT
IN (
    SELECT booking_id
    FROM tbl_cust_booking
);
slsn1g29

slsn1g291#

下面是一个修改自documentation的示例,该示例针对一个事件执行多个查询:

delimiter |

CREATE EVENT e_daily
    ON SCHEDULE
      EVERY 1 DAY
    COMMENT 'Saves total number of sessions then clears the table each day'
    DO
      BEGIN
    INSERT INTO tbl_bookings_released
    (
        id, row, seatnum, price,theatre_id, play_id, show_id, showtime, show_date, 
        isbooked, inserted_at, inserted_from, booking_num, tot_price, subzone_id, 
        zone_id, txn_id
    )
    SELECT 
        id, row, seatnum, price,theatre_id, play_id, show_id, showtime, 
        show_date, isbooked, inserted_at, inserted_from, booking_num, 
        tot_price, subzone_id, zone_id, txn_id
    FROM tbl_bookings
    WHERE (
        UNIX_TIMESTAMP( NOW( ) ) - UNIX_TIMESTAMP( inserted_at )
    ) /60 > 2
    AND booking_num NOT
    IN (
        SELECT booking_id
        FROM tbl_cust_booking
    );

    DELETE
    FROM tbl_bookings
    WHERE (
        UNIX_TIMESTAMP( NOW( ) ) - UNIX_TIMESTAMP( inserted_at )
    ) /60 > 2
    AND booking_num NOT
    IN (
        SELECT booking_id
        FROM tbl_cust_booking
    );

      END |

delimiter ;
lsmd5eda

lsmd5eda2#

如果您正在使用phpmyadmin创建事件,请在开始和END标记

中添加多个查询

pdtvr36n

pdtvr36n3#

DELIMITER @@;

CREATE EVENT test_event_03
ON SCHEDULE EVERY 1 MINUTE
STARTS CURRENT_TIMESTAMP
ENDS CURRENT_TIMESTAMP + INTERVAL 72 HOUR
DO
BEGIN

    UPDATE complete_exam_set 
   SET live_status = '1' 
   WHERE test_time < CURRENT_TIMESTAMP ;

   UPDATE complete_exam_set 
   SET live_status = '2' 
   WHERE end_time < CURRENT_TIMESTAMP ;

END;
@@;

DELIMITER ;

为什么那个红十字快结束了,怎么了?

7cwmlq89

7cwmlq894#

你得到了红色的x,因为你在最后一行使用了分号。

相关问题