How to decide in what hour event starts in phpmyadmin? - mysql

if i'm trying to create an event in this page :
Now i have set it to run every 1 month, but how do i set at what time?
i want the report to save all record from last month into a table using
l.created_on <= DATE_SUB(NOW(), INTERVAL 1 month)
So it will run at 1/1/12 00:00:00 i guess but i'd rather wait 2 minutes just in case something was inserted in the exact same moment, so i can i set that while using phpMyAdmin?

What I meant was set the time here... That's a common trigger for date as well as time

Related

Why change input interval value on mysql?

I want create event that execute every monday 00:00:00.
I created event but don't work. In my Opinion, reason is interval value.
I use EVERY 1 WEEK option but mysql display 7 interval value. Is it right reason? If yes why don't input correct value?
This is my create query
CREATE EVENT IF NOT EXISTS clear_temp_week_news_count
ON SCHEDULE every 1 week starts CURRENT_DATE + INTERVAL 7 - WEEKDAY(CURRENT_DATE) DAY
on completion preserve enable
DO
delete from temp_week_news_count;
This is result when use "show events;"
enter image description here
I did check db status on with "SHOW VARIABLES LIKE 'event%';"

Create delete event that deletes older records on daily basis

I want to delete the records that is older than 1 day. What is the best way to achieve it? I have never used event before so i am having little problem.
For eg: I want to delete records where start_time is older than 1 day.
by doing some research i got to this point.
CREATE EVENT deleteRecords
ON SCHEDULE EVERY 1 DAY
ON COMPLETION PRESERVE
DO
DELETE FROM databaseName.tableName WHERE start_time < DATE_SUB(NOW(),
INTERVAL 1 DAY)
There are many ways on how to achieve your desired result.
//sample 1
DELETE FROM databaseName.tableName WHERE DATE_ADD(start_time,INTERVAL 1 DAY) < NOW();
//sample 2
DELETE FROM databaseName.tableName WHERE ADDTIME(start_time,"1 00:00:01") =< NOW();
The code in your post is okay to use too. But don't forget to backup data first if you're unsure of what you're doing.
Reference Date and Time Functions

How to UPDATE data in MySQL after a scheduled time

I am new to SQL and I want to update the Amount in the amount field. For example in every month in 3 months I want to add 100 to THE 500 in the amount field in the database. And when the 3rd month comes It will automatically transfer the data to another table
This is the the code I have done so far, but its not working
CREATE EVENT myevent
ON SCHEDULE EVERY 1 MINUTE
STARTS CURRENT_TIMESTAMP
ENDS CURRENT_TIMESTAMP + INTERVAL 2 MINUTE
DO
UPDATE message
SET amount*3
WHERE date = NOW();
Try to change the update command into:
UPDATE Message
SET amount = amount + 100
WHERE DATEDIFF(CURDATE(), date) >=value

How do I increment a value in MySQL to a certain time in the future?

I am trying to update a DATETIME field in a table. What I want for it is to always have three hours before it expires.
My current code is:
UPDATE mindcore_sessions
SET session_time = DATE_ADD(session_time, INTERVAL 3 HOUR)
WHERE session_id = '$sessionId';
An example value would be: 2013-02-11 00:00:00. If I run this query, it will change to: 2013-02-11 03:00:00. Which is correct. But, if it is run again, it changes to 2013-02-11 06:00:00 and so on.
What I want for it is to always be only three hours ahead. This is hard to explain, but I just want it to stay the same if it is run again. And if it is run again 1 minute later, I want it to increment by just one minute.
Sorry if the question is verbose, but I am utterly confused (and it is late!).
Thanks :)
Instead of adding three(3) hours to the last session_time, add three(3) hours to NOW():
update mindcore_sessions
set session_time = DATE_ADD(NOW(), INTERVAL 3 HOUR)
where session_id = '$sessionId';`
Add your interval to NOW() instead of adding to the current value:
UPDATE mindcore_sessions SET session_time = DATE_ADD(NOW(), INTERVAL 3 HOUR) WHERE session_id = '$sessionId';
There is no way in mysql to know if the same query was executed before.
The only possible solution would be to save the original value in an extra column, and use this column to check if the value was changed in the past.
If you want the value to be ahead 3 hours from now, you can simply do this:
DATE_ADD(NOW(), INTERVAL 3 HOUR)

MySQL Event Scheduler on a specific time everyday

Here's my query
CREATE EVENT
RESET ON SCHEDULE AT TIMESTAMP DO
UPDATE `ndic`.`students`
SET `status` = '0';
How can I update status to "0" at 1 pm every day.
What can I use instead of TIMESTAMP?
This might be too late for your work, but here is how I did it. I want something run everyday at 1AM - I believe this is similar to what you are doing. Here is how I did it:
CREATE EVENT event_name
ON SCHEDULE
EVERY 1 DAY
STARTS (TIMESTAMP(CURRENT_DATE) + INTERVAL 1 DAY + INTERVAL 1 HOUR)
DO
# Your awesome query
The documentation on CREATE EVENT is quite good, but it takes a while to get it right.
You have two problems, first, making the event recur, second, making it run at 13:00 daily.
This example creates a recurring event.
CREATE EVENT e_hourly
ON SCHEDULE
EVERY 1 HOUR
COMMENT 'Clears out sessions table each hour.'
DO
DELETE FROM site_activity.sessions;
When in the command-line MySQL client, you can:
SHOW EVENTS;
This lists each event with its metadata, like if it should run once only, or be recurring.
The second problem: pointing the recurring event to a specific schedule item.
By trying out different kinds of expression, we can come up with something like:
CREATE EVENT IF NOT EXISTS `session_cleaner_event`
ON SCHEDULE
EVERY '1 13' DAY_HOUR
COMMENT 'Clean up sessions at 13:00 daily!'
DO
DELETE FROM site_activity.sessions;
Update, long over-due: The interval with the unit "DAY_HOUR" takes an expression that looks like 'day-number hour-number'. I have updated the oft-accepted solution to match that. Thanks, commenters for clarifying!
Try this
CREATE EVENT event1
ON SCHEDULE EVERY '1' DAY
STARTS '2012-04-17 13:00:00' -- should be in the future
DO
-- your statements
END
DROP EVENT IF EXISTS xxxEVENTxxx;
CREATE EVENT xxxEVENTxxx
ON SCHEDULE
EVERY 1 DAY
STARTS (TIMESTAMP(CURRENT_DATE) + INTERVAL 1 DAY + INTERVAL 1 HOUR)
DO
--process;
¡IMPORTANT!->
SET GLOBAL event_scheduler = ON;
My use case is similar, except that I want a log cleanup event to run at 2am every night. As I said in the comment above, the DAY_HOUR doesn't work for me. In my case I don't really mind potentially missing the first day (and, given it is to run at 2am then 2am tomorrow is almost always the next 2am) so I use:
CREATE EVENT applog_clean_event
ON SCHEDULE
EVERY 1 DAY
STARTS str_to_date( date_format(now(), '%Y%m%d 0200'), '%Y%m%d %H%i' ) + INTERVAL 1 DAY
COMMENT 'Test'
DO
CREATE EVENT test_event_03
ON SCHEDULE EVERY 1 MINUTE
STARTS CURRENT_TIMESTAMP
ENDS CURRENT_TIMESTAMP + INTERVAL 1 HOUR
DO
INSERT INTO messages(message,created_at)
VALUES('Test MySQL recurring Event',NOW());