MySQL sheduler query - mysql

I need to delete+update in one MySQL sheduler event
queries:
DELETE FROM access WHERE d > 0;
UPDATE access SET d = 1 WHERE d = 0;
how to combine them in one MySQL event?

First set the event_scheduler ON.
SET GLOBAL event_scheduler = ON;
Now create the event (The event will be scheduled in 5 Minutes
interval)
DROP EVENT IF EXISTS `deleteUpdateEvent`;
delimiter $$
CREATE EVENT `deleteUpdateEvent` ON SCHEDULE EVERY 5 MINUTE STARTS '2016-05-17 00:00:00'
ON COMPLETION PRESERVE ENABLE
DO
BEGIN
DELETE FROM access WHERE d > 0;
UPDATE access SET d = 1 WHERE d = 0;
END$$
delimiter;
Example:
Suppose you have the following data in your access table:
id d
1 66
2 71
3 -6
4 -7
5 0
When the event finishes it's execution (FIRST RUN) your access table would look like below:
id d
3 -6
4 -7
5 1
After second run:
access table:
id d
3 -6
4 -7
Note:
For the subsequent runs your table won't change unless you have new data having >= 0 value

You can try like this:
DELIMITER $$
CREATE EVENT `myEvent`
ON SCHEDULE EVERY 1 MINUTE STARTS 'yourStartTime'
ON COMPLETION NOT PRESERVE ENABLE
DO
BEGIN
DELETE FROM access WHERE d > 0;
UPDATE access
SET d = 1
WHERE d = 0;
END $$
DELIMITER ;

Related

MySQL Trigger running total

I have a table, Transactions1, with 3 Columns
ID,NetProfit and RunningTotal
I'm trying to create a trigger that automatically updates the RunningTotal on new entry as shown below:
ID NetProfit RunningTotal
1 100 100
2 50 150
3 200 350
I have tried the following code but can't seem to get it to work:
CREATE TRIGGER RTTrigger BEFORE INSERT ON Transactions1
FOR EACH ROW
set #RT1 := 0;
update Transactions1
set RunningTotal = (#RT1 := #RT1 + 'NetProfit')
order by ID;
I get the error:
Not allowed to return a resultset from a trigger
I have to mention that i'm new to Mysql and PhPMyAdmin:-)
Thanx in advance

Getting a minute interval between start and end time mysql

Id | starttime | endtime |
1 | 1999-05-07 15:00 | 1999-05-07 16:45 |
How do I get it interval of 1 minute from the start time to the end time.
So I want something like this 15:01, 15:02.
Please any help will be appreciated new to sql and trying to learn it as best I can. Thanks
NOTE :
I don't want the whole minute. I want to get every minute from start to end I need it to count the users that logged in every minute of the game if that makes any sense
SELECT *,TIMESTAMPDIFF(MINUTE,starttime,endtime) FROM Table1
Create this function
CREATE function getTimeAll(id1 int)
RETURNS VARCHAR(65535) DETERMINISTIC
BEGIN
DECLARE time1 varchar(65535);
DECLARE intrvlMin int;
DECLARE c int;
SET intrvlMin=(SELECT TIMESTAMPDIFF(MINUTE,starttime,endtime) FROM Table1 WHERE id = id1);
SET c=1;
SET time1 = '';
While c < intrvlMin
do
SET time1=CONCAT(time1,(SELECT date_format(date_add(starttime,interval c minute),'%H:%i') FROM Table1 WHERE id = id1),',');
SET c = c + 1;
end WHILE;
SET time1=CONCAT(time1,(SELECT date_format(date_add(starttime,interval c minute),'%H:%i') FROM Table1 WHERE id = id1));
RETURN time1;
END ;
Then Run
SELECT `Id`, `starttime`, `endtime`,getTimeAll(`Id`) AS Times FROM Table1;
output
Id starttime endtime Times
1 1999-05-07 15:00:00 1999-05-07 16:45:00 15:01,15:02,15:03,15:04,15:05,15:06,15:07,15:08,15:09,15:10,15:11,15:12,15:13,15:14,15:15,15:16,15:17,15:18,15:19,15:20,15:21,15:22,15:23,15:24,15:25,15:26,15:27,15:28,15:29,15:30,15:31,15:32,15:33,15:34,15:35,15:36,15:37,15:38,15:39,15:40,15:41,15:42,15:43,15:44,15:45,15:46,15:47,15:48,15:49,15:50,15:51,15:52,15:53,15:54,15:55,15:56,15:57,15:58,15:59,16:00,16:01,16:02,16:03,16:04,16:05,16:06,16:07,16:08,16:09,16:10,16:11,16:12,16:13,16:14,16:15,16:16,16:17,16:18,16:19,16:20,16:21,16:22,16:23,16:24,16:25,16:26,16:27,16:28,16:29,16:30,16:31,16:32,16:33,16:34,16:35,16:36,16:37,16:38,16:39,16:40,16:41,16:42,16:43,16:44,16:45
DEMO
https://www.db-fiddle.com/f/nge35TSTWyd3pLSw6X1TE4/2
You might need to use loop ,
Firstly
select TIMESTAMPDIFF(minute,starttime,endtime) from table1
Here you will get 105
This calculates number of minutes between 2 times
SELECT date_format(date_add(starttime,interval 1 minute),'%H:%i') from table1
SELECT date_format(date_add(starttime,interval 2 minute),'%H:%i') from table1
SELECT date_format(date_add(starttime,interval 3 minute),'%H:%i') from table1
loop this till 105 times with increment interval time

MySQL events doesn't run eventhough every settings is enable

I have created simple event but it doesn't run:
DELIMITER |
CREATE EVENT myevent3
ON SCHEDULE EVERY 1 second
STARTS current_timestamp()
ENDS CURRENT_TIMESTAMP + INTERVAL 1 DAY
DO
BEGIN
UPDATE hiberante.animal SET name = name + 'a' where id = 6;
END |
DELIMITER ;
I have done:
SET GLOBAL event_scheduler = ON;
Also done:
SELECT * FROM INFORMATION_SCHEMA.EVENTS;
and
SELECT * FROM mysql.event;
And it still doesn't want to start working.
In my process list:
SHOW PROCESSLIST;

Drop a MySQL event if a condition is met

I've a table (table_rne) like
id maxv curv
1 200 20
And I've created an event (increase_curv) which will increase 'curv' by 10 every 1 minute
CREATE EVENT increase_curv
ON SCHEDULE EVERY 8 SECOND
DO
UPDATE table_rne SET curv = curv + 10 WHERE id='1';
But I want to drop the event when 'curv' reaches maxv (200). How to do that using MySQL conditions?
You can try something like this;
CREATE EVENT increase_curv
ON SCHEDULE EVERY 8 SECOND
DO
IF curv < 200 THEN
UPDATE table_rne SET curv = curv + 10 WHERE id='1'
ELSE
DROP EVENT [IF EXISTS] increase_curv
END IF;

mysql stored procedure not firing from event scheduler

Can someone please tell me what's wrong with this sp. The logic seems to be ok, but when i check back in my table it doesn't work at all.
DELIMITER //
DROP PROCEDURE IF EXISTS add_zero_yearly_sales_proc //
CREATE PROCEDURE add_zero_yearly_sales_proc()
READS SQL DATA
BEGIN
DECLARE num_of_sales INT DEFAULT 0;
DECLARE last_ins_date DATETIME;
DECLARE done INT DEFAULT 0;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
SET last_ins_date = DATE_SUB(NOW(), INTERVAL 2 YEAR);
ins_loop: WHILE last_ins_date < NOW() DO
SELECT COUNT(sales_amount_sold) INTO num_of_sales
FROM yearly_sales
WHERE sales_date_sold BETWEEN DATE_FORMAT(last_ins_date,'%Y-%m-01 00:00:01') AND DATE_FORMAT(LAST_DAY(last_ins_date),'%Y-%m-%d 23:59:59');
IF num_of_sales = 0 THEN
INSERT INTO yearly_sales(sales_date_sold, sales_amount_sold,sales_quantity)
VALUES (CONCAT(DATE_FORMAT(last_ins_date,'%Y-%m-01 00:00:01')),0, 0);
END IF ;
SET num_of_sales = 0;
SET last_ins_date = DATE_ADD(last_ins_date, INTERVAL 1 MONTH);
END WHILE ins_loop;
SET done = 0;
END ;//
DELIMITER ;
I've created an event that fires every hour to call this procedure. The procedure is supposed to check if there are months in the yearly_sales table without any sales values in them, and if so add 0 values for the amount & quantity, and the beginning of the month for the date. I've checked back but it doesn't seem to work.
Also here's the event i created to call it hourly
DELIMITER //
CREATE
EVENT `hourly_sales_evt`
ON SCHEDULE EVERY 1 HOUR STARTS DATE_FORMAT(NOW(),'%Y-%m-%d %H:55:00')
ON COMPLETION PRESERVE
DO BEGIN
CALL add_zero_yearly_sales_proc();
END //
DELIMITER ;
May be global event scheduler is in stopped/disabled state.
To turn on event scheduler, run any of the following:
SET GLOBAL event_scheduler = ON;
SET ##global.event_scheduler = ON;
SET GLOBAL event_scheduler = 1;
SET ##global.event_scheduler = 1;
When the Event Scheduler is ON, the event scheduler thread is listed in the output of SHOW PROCESSLIST as a daemon process, and its state is represented as shown here:
mysql> SHOW PROCESSLIST\G
*************************** 1. row ***************************
Id: 1
User: root
Host: localhost
db: NULL
Command: Query
Time: 0
State: NULL
Info: show processlist
*************************** 2. row ***************************
Id: 2
User: event_scheduler
Host: localhost
db: NULL
Command: Daemon
Time: 3
State: Waiting for next activation
Info: NULL
2 rows in set (0.00 sec)
Once the Event Scheduler is set ON, you would see it working.
Refer to : MySQL Event Scheduler Configuration
The procedure looks OK, the logic should work. But, I want to ask you - why do you check sales_date_sold from first second -DATE_FORMAT(last_ins_date,'%Y-%m-01 00:00:01'), shouldn't it be DATE_FORMAT(last_ins_date,'%Y-%m-01 00:00:00')?
Another point: you execute SELECT statement many times in the loop, it is not effective. Try to create additional (maybe temporary table) with month numbers and join two tables to find out months without any sales. In this case you will achieve result in one step.
I'm taking a guess here without doing much digging, but I think this is your issue:
READS SQL DATA
...
INSERT INTO yearly_sales(sales_date_sold, sales_amount_sold,sales_quantity)
VALUES (CONCAT(DATE_FORMAT(last_ins_date,'%Y-%m-01 00:00:01')),0, 0);