I want to execute the following queries with help of Mysql event But when I add the delete statement in the event and try to create it, gives me Mysql Error. If I chose to skip the delete statement the event gets created without any problem.
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
);
MySQL optionally allows having multiple statements in one statement string, but it requires special handling. Multiple statements or multi queries must be executed with mysqli::multi_query(). The individual statements of the statement string are separated by semicolon.
MySQL Events are tasks that run according to a schedule. Therefore, we sometimes refer to them as scheduled events. When you create an event, you are creating a named database object containing one or more SQL statements to be executed at one or more regular intervals, beginning and ending at a specific date and time.
How to run Multiple SQL Queries in MySQL Workbench explains how you can run multiple statements in single query window by separating them with semicolon ; You can have different types of statements such as delete, update or insert in single query window and all can be executed with single click.
Here is an example modified from the documentation that execute multiple queries for an event:
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 ;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With