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

时间:2015-11-26 06:28:52

标签: mysql mysql-event

我想在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
);

3 个答案:

答案 0 :(得分:11)

以下是从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 ;

答案 1 :(得分:6)

如果您使用phpmyadmin创建事件,则在BEGIN和END标记内添加多个查询 enter image description here

答案 2 :(得分:0)

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 ; 

为什么红十字即将到END 怎么了?

error image