MySQL Event won't execute - mysql

Can anyone tell me why this event will not execute?
CREATE EVENT add_recurring
ON SCHEDULE EVERY 1 MINUTE
STARTS '2015-04-17 06:01:00'
DO INSERT INTO payment (amount, note, custID, type)
SELECT amount, note, custID, 'C' FROM recurring WHERE day = DATE_FORMAT(CURRENT_DATE(), '%e')
Here is what the recurring table looks like:
And here is the payment table which is not getting any of these values entered by the event:

Try these, assuming start date is future
try adding BEGIN , END and DELIMITER
delimiter |
CREATE EVENT add_recurring
ON SCHEDULE
EVERY 1 MINUTE
STARTS '2015-04-17 06:01:00'
DO
BEGIN
INSERT INTO payment (amount, note, custID, type), SELECT amount, note, custID, 'C' FROM recurring WHERE day = DATE_FORMAT(CURRENT_DATE(), '%e')
END |
delimiter ;
Ensure that even schedular is turned on.
SET GLOBAL event_scheduler = ON;
SET GLOBAL event_scheduler = 1;
OR in my.cnf under [mysqld] section event_scheduler=ON

I assume you have changed your system clock? The query is 2 months from now...

Related

Updating both parent and child table in stored procedure

MySQL Version: '5.7.37-0ubuntu0.18.04.1'
I have the goals of
Updating rows in table ost_ticket after one week of no new activity
Update or Insert into table ost_ticket__cdata one row for each row updated in ost_ticket
I will be using MySQL Event Scheduler to call the code that will accomplish the above two goals on an hourly basis.
My tendency is to put the code in a stored procedure, which is then called by the event scheduler.
My challenge is that I'm not sure how to transition from goal 1 to goal 2. How can I Update / insert into the child table for only those rows affected on the parent table, and only as a result of the event scheduler job (in other words, I don't want to use a trigger any time the parent table is updated, only when the "one-week-old" condition is satisfied)
Draft of the stored procedure with my rambling commentary is below...
USE `osticket`;
DROP procedure IF EXISTS `AutoClose';
DELIMITER //
Create Procedure AutoClose ()
Begin
# GOAL 1: UPDATE ROWS THAT ARE ONE WEEK OLD...
UPDATE ost_ticket t
SET
t.status_id = 3, # 3 = "Closed"
t.closed = date_add(t.lastupdate, interval 7 day),
t.updated = date_add(t.lastupdate, interval 7 day)
t.lastupdate = date_add(t.lastupdate, interval 7 day),
WHERE t.status_id = 2 # 2 = "Resolved"
AND now() >= date_add(t.lastupdate, interval 7 day);
# GOAL 2: UPSERT ADDITIONAL INFORMATION ON CHILD TABLE
# DONT THINK THIS WILL WORK HERE... HOW DO I RUN THE FOLLOWING CODE
# FOR EACH ROW AFFECTED BY THE UPDATE ABOVE?
INSERT INTO ost_ticket__cdata (ticket_id, subject, priority, autoclosed)
ON DUPLICATE KEY UPDATE autoclosed = '1';
values
(x, y, 2, 1)
End //
DELIMITER;

Mysql application logging. Limit the number of rows logged by deleting the old logs

I have a log table in my Mysql database to log the events happening in my application. I want to limit the maximum number of rows in that table by deleting the old events(rows). Before deleting I want to save(backup) the old data which is going to remove to an external file. I want to run this trigger or event or whatever used to do this every 5 days or so automatically.
SET GLOBAL event_scheduler = ON;
delimiter |
CREATE event cleaner ON schedule every 7 day enable do
BEGIN
DECLARE maxtime TIMESTAMP;
DECLARE logCount integer;
SET maxtime = CURRENT_TIMESTAMP - interval 180 day;
SET logCount = (Select count(*) from eventlogs);
DELETE FROM eventlogs WHERE eventlogs.TimeStamp < maxtime AND logCount > 1000 ; END
| delimiter ;

Mysql Add days onto date from two different columns

Hi my problem relates to adding days onto a date from two different tables in MySql in the Mamp environment.
Membership type to Membership transaction is 1 to many
The link is type_id
Date is in yyyy/mm/dd format also as this is the only format that Mamp will allow from my research.
I want a new end date column that links to the column duration from the membership type table. I want to add Duration_day onto start_date to produce an end date that matches up with the type_id. (So they link up to give the correct end date)
I want it to be automatically calculated when the start date and type-id are saved
Any help would be appreciated
Thanks
You will need a trigger on INSERT/UPDATE - a calculated column (for MySQL v5.7.6+) will not work in your case (it can only refer to columns in the same table).
The 2 triggers may look like this
CREATE DEFINER = 'root'#'%' TRIGGER `m_duration_ins_tr1` BEFORE INSERT ON `membership`
FOR EACH ROW
BEGIN
DECLARE duration INTEGER;
SELECT m_duration INTO duration FROM membership_type WHERE id = NEW.type_id;
SET NEW.end_date := DATE_ADD(NEW.start_date, INTERVAL duration DAY);
END;
CREATE DEFINER = 'root'#'%' TRIGGER `m_duration_ins_tr1` BEFORE UPDATE ON `membership`
FOR EACH ROW
BEGIN
DECLARE duration INTEGER;
SELECT m_duration INTO duration FROM membership_type WHERE id = NEW.type_id;
SET NEW.end_date := DATE_ADD(NEW.start_date, INTERVAL duration DAY);
END;

Mysql Event Scheduler using local variable

I am trying to create a report schedule in mysql, but the query depends on the local variable fdate. How can I have mysql load the fdate variable and then run the query in the 2nd event? Thanks
CREATE EVENT fdate
ON SCHEDULE
EVERY 1 DAY
starts '2018-03-26 07:30:00'
Do
set #fdate = 2018031; #EOM prior month
create event report_1 on SCHEDULE
EVERY 1 day
starts '2018-03-26 07:31:00'
DO
<Here is the Query depending up fdate>
I think each event executes in a new session, and user-defined variables have session scope. So you can't really use those variables in events and have their value survive from one event execution to another.
Here's a test to show it:
mysql> create table log ( i int );
mysql> create event e on schedule every 1 minute do
-> insert into log set i = #a := coalesce(#a+1, 1);
If the #a variable retains its value from one event execution to the next, we'll get a series of incrementing values in my log table.
A few minutes later, we see that we don't get incrementing values. Each execution starts the counter over at 1.
mysql> select * from log;
+------+
| i |
+------+
| 1 |
| 1 |
| 1 |
+------+
But you can DECLARE a local variable, and use it in a compound statement body for an event.
CREATE EVENT fdate
ON SCHEDULE
EVERY 1 DAY
STARTS '2018-03-26 07:30:00'
DO BEGIN
DECLARE fdate INT;
SET fdate = DATE_FORMAT(LAST_DAY(CURDATE() - INTERVAL 1 MONTH), '%Y%m%d');
# following query uses fdate
END

Converting UTC 12 hour time format to indian time using Mysql

I am writing an event scheduler in Mysql for generating tickets 3 hour prior to the user travel time. Mysl is installed in server where the server time is in UTC and 12 hour format with 5:30 hour difference with IST time. I have one user whose travel time is 7:30 in the morning, so i need to generate ticket for this user at 4:30 in the morning. I am getting current server time and converting to IST and comparing travel time and current converted time difference is 3 hours. But some how its failing and always ticket creating at 10 am in the morning. Below is my complete event writtent in Mysql,
DELIMITER $$
ALTER EVENT `Auto_Generated_Ticket` ON SCHEDULE EVERY 1 MINUTE STARTS CURRENT_TIMESTAMP ON COMPLETION PRESERVE ENABLE DO BEGIN
DECLARE UserId INT;
DECLARE v_finished INT DEFAULT 0;
DECLARE GetDate DATE DEFAULT DATE(NOW());
/*get all active user's who's tariff enddate is > today and available journeys > 0 and pass-status=4(delivered)*/
DECLARE ActiveUserId CURSOR FOR
SELECT UT.user_id FROM `um.user_trs.tariff` UT
INNER JOIN `um.user` U ON U.user_id=UT.user_id
INNER JOIN `um.user_ps.pass` UP ON UP.user_id=UT.user_id
INNER JOIN `ps.pass` P ON P.pass_id=UP.pass_id
WHERE UT.end_date >=DATE(NOW()) AND UT.available_journeys > 0 AND UT.current_balance>0 AND P.status_id=4
GROUP BY UT.user_id;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET v_finished=1;
SET #GetTime= DATE_FORMAT(NOW(),'%Y-%m-%d %h:%i:%s %p');
OPEN ActiveUserId;
get_userid:LOOP
FETCH ActiveUserId INTO UserId;
IF v_finished=1 THEN
LEAVE get_userid;
END IF;
SET #StartTime=(SELECT RS.start_time FROM `um.user_rm.route` UR
INNER JOIN `rm.route_schedule` RS ON RS.route_schedule_id=UR.route_schedule_id
WHERE UR.user_id=UserId
ORDER BY ABS( TIMEDIFF( RS.`start_time`, TIME(CONVERT_TZ(#GetTime,'+00:00','+05:30')) ) ) LIMIT 1);
SET #TimeDiff=(HOUR(TIMEDIFF(#StartTime,TIME(CONVERT_TZ(#GetTime,'+00:00','+05:30')))));
/*if time difference between current time and schedule start time is 3 hours then generate ticket for the user for that particular schedule*/
IF (#TimeDiff =3) THEN
/*IF (#TNumber IS NULL) THEN*/
IF NOT EXISTS(SELECT ticket_number FROM `um.user_ts.ticket` WHERE route_id=#RouteId AND route_schedule_id=#RoutScheduleId
AND user_id=UserId AND DATE(date_of_issue)=#ISTDATE) THEN
INSERT INTO `um.user_ts.ticket`
(
`user_id`,`route_id`,`route_schedule_id`,`ticket_number`,`date_of_issue`,`is_cancelled`,
`amount_charged`,`user_tariff_id`,`opening_balance`,`is_auto_generated`
)
VALUES
(
UserId,#RouteId,#RoutScheduleId,#TicketNumber,CONVERT_TZ(UTC_TIMESTAMP(),'+00:00','+05:30'),
FALSE,#PerJourneyCost,#UserTariffId,#TariffCurrentBalance,1
);
END IF;/*end of route and schedule check*/
END IF; /*end of time difference check*/
END LOOP get_userid;
CLOSE ActiveUserId;
END$$
DELIMITER ;
Please let me know if any other way to convert time or any mistake which i am not noticing in the above query.
Regards
Sangeetha
There is a dedicated mysql command to do so. CONVERT_TZ(dt,from_tz,to_tz)
See here https://dev.mysql.com/doc/refman/5.6/en/date-and-time-functions.html#function_convert-tz
From their docs:
CONVERT_TZ() converts a datetime value dt from the time zone given by
from_tz to the time zone given by to_tz and returns the resulting
value
Maybe the warning applies to you ?
Warning
Do not use a downloadable package if your system has a zoneinfo
database. Use the mysql_tzinfo_to_sql utility instead. Otherwise, you
may cause a difference in datetime handling between MySQL and other
applications on your system.
I would break it down to a minimalistic example and see if this works.
Here's how I would do it to test the basics:
Simple example:
SELECT CONVERT_TZ('2016-05-24 12:00:00','GMT','MET');
So you could build a query to determine the timediff like this:
HOUR(TIMEDIFF(
CONVERT_TZ('2016-05-24 12:00:00','GMT','MET'),
'2016-05-24 12:00:00'
))