I created a new table in MySQL, I want to make changes in ( column1 and column2 ) columns values automatically in sql.
I tried this code, but their is no changes in table after 6 days.
CREATE EVENT weekly
ON SCHEDULE
EVERY 6 days
STARTS DATE(NOW()) + INTERVAL (6 DAYS(NOW())+1) DAY
ON COMPLETION PRESERVE
DO
UPDATE INTO `MyTable` (`value1`, `value2`)
SELECT `column1` , `column2`)
WHERE `column1` >= NOW() - INTERVAL 6*24 HOUR;
Your UPDATE statement is not correct. It should be like below -
UPDATE `MyTable`
SET `column1` = ???
,`column2` = ???
WHERE `column1` >= NOW() - INTERVAL 6*24 HOUR;
Replace ??? with the the value you want to update in col1 and col2
Not sure how you want to update the value for column1 and 2. But the event should look like this:
DELIMITER //
CREATE EVENT IF NOT EXISTS daily_update
ON SCHEDULE
EVERY 1 DAY STARTS DATE(NOW())
ON COMPLETION PRESERVE
COMMENT 'Update myTable after 6 days'
DO
BEGIN
UPDATE MyTable
SET column1='value1',
column2='value2'
WHERE column1>= (NOW() - INTERVAL 6*24 HOUR);
END
//
Related
I'm trying to create a table and set the default value to now() + 24 hours. I'm getting syntax errors when trying to do this.
This doesn't work
CREATE TABLE IF NOT EXISTS `my_table` (
`my_table_id` CHAR(36) BINARY NOT NULL ,
`expiration_time` DATETIME NOT NULL DEFAULT (NOW() + INTERVAL 24 HOUR),
PRIMARY KEY (`my_table_id`)
) ENGINE=InnoDB;
Although this does work SELECT NOW() + INTERVAL 24 HOUR; so i'm not sure why it doesn't work when trying to create a table.
Expressions for defaults are not supported in MySQL 5.7.
You can implement a "default" expression in a trigger such as the following:
CREATE TRIGGER my_awesome_trigger BEFORE INSERT ON my_table
FOR EACH ROW
SET NEW.expiration_time = COALESCE(NEW.expiration_time, NOW() + INTERVAL 24 HOUR));
I have a timestamp column in my MySQL table.
I'm wanting to set this timestamp to a random time within the past 24 hours for all rows in the table.
I know I can update all the rows doing this:
UPDATE table SET timestamp =
But I can't find if there's a way to set a random timestamp that's occurred within the past 24 hours so that each row has a different time.
You can use:
UPDATE table
SET timestamp = now() - interval floor((24*60*60)*rand()) second;
You can use Unixtimestqamps for that
UPDATE table1
SET timestamp = (SELECT TIMESTAMPADD(SECOND,
FLOOR(RAND() * TIMESTAMPDIFF(SECOND, NOW() - INTERVAL 1 DAY, NOW()))
, NOW() - INTERVAL 1 DAY));
You can try:
Update table set timestamp = select(cast((sysdate() - floor(rand()*24)) AS Datetime));
Check this might work for you.
update table name set timestamp = now() - interval floor(rand()*(60*60*24*2)) second;
Output:
you will get the current timestamp- between 0 seconds and two days.
If you want to change 2 days to 3 days or any days just need to change(60*60*24***Days enter here**))
How to create event Auto-Delete some row 1 day after inserting that row?
I have 3 fields:
id varchar
name varchar
timestamp current_timestamp()
Please take a look at Event: https://dev.mysql.com/doc/refman/5.7/en/events-syntax.html
in your case:
you can try this one:
-- create a timer to update this table automatically
DROP EVENT IF EXISTS `et_update_your_trigger_name`;
CREATE EVENT `et_update_your_trigger_name` ON SCHEDULE EVERY 1 MINUTE
STARTS '2010-01-01 00:00:00'
DO
DELETE FROM `DB_NAME`.`table_name` where DATEDIFF(now(),`timestamp`) > 1;
ALTER EVENT `et_update_your_trigger_name` ON COMPLETION PRESERVE ENABLE;
this sql code create a trigger, and execute every minutes.
Here's your scripts.
Insert into table1 (id, name, timestamp) values (1, 'test', now())
after insert
Delete from table1 WHERE timestamp < now() - interval 1 day;
Please try like this:
DELETE FROM table1
WHERE date < DATE_SUB(NOW(), INTERVAL 1 DAY)
I'm trying to create this event but I can't find the syntax error.
CREATE
EVENT respaldoRegRawInformeAperturaLocal
ON SCHEDULE EVERY 1 DAY STARTS '2017-08-03 22:00:00'
DO BEGIN
-- INSERT INTO BACKUP TABLE
INSERT INTO regRawInformeAperturaLocalBACKUP (regRawInformeAperturaLocalId, rawInformeAperturaLocal, done, date, deviceId)
-- GET DATA
SELECT regRawInformeAperturaLocalId, rawInformeAperturaLocal, done, date, deviceId
FROM regRawInformeAperturaLocal
WHERE regRawInformeAperturaLocal.date < DATE_SUB(NOW(), INTERVAL 5 MONTH);
-- DELETE DATA FROM ORIGINAL TABLE
DELETE FROM regRawInformeAperturaLocal WHERE regRawInformeAperturaLocal.date < DATE_SUB(NOW(), INTERVAL 5 MONTH);
END;
[CODE EDIT 1]
CREATE
EVENT respaldoRegRawInformeAperturaLocal
ON SCHEDULE EVERY 1 DAY STARTS '2017-08-03 22:00:00'
DO BEGIN
-- INSERT INTO BACKUP TABLE
INSERT INTO regRawInformeAperturaLocalBACKUP (regRawInformeAperturaLocalId, rawInformeAperturaLocal, done, date, deviceId)
-- GET DATA
SELECT regRawInformeAperturaLocalId, rawInformeAperturaLocal, done, date, deviceId
FROM regRawInformeAperturaLocal
WHERE date < DATE_SUB(NOW(), INTERVAL 5 MONTH);
-- DELETE DATA FROM ORIGINAL TABLE
DELETE FROM regRawInformeAperturaLocal WHERE date < DATE_SUB(NOW(), INTERVAL 5 MONTH);
END;
[EDIT]
This is the syntax error message:
[Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 11
I could make it work:
I have to add the DELIMITER and change the syntaxis from END; to END|
Anyway here's the code:
DELIMITER |
CREATE
EVENT IF NOT EXISTS respaldoRegRawInformeAperturaLocal
ON SCHEDULE EVERY 1 DAY STARTS '2017-08-03 22:00:00'
DO BEGIN
-- INSERT INTO BACKUP TABLE
INSERT INTO regRawInformeAperturaLocalBACKUP (regRawInformeAperturaLocalId, rawInformeAperturaLocal, done, date, deviceId)
-- GET DATA
SELECT regRawInformeAperturaLocalId, rawInformeAperturaLocal, done, date, deviceId
FROM regRawInformeAperturaLocal
WHERE date < DATE_SUB(NOW(), INTERVAL 5 MONTH);
-- DELETE DATA FROM ORIGINAL TABLE
DELETE FROM regRawInformeAperturaLocal WHERE date < DATE_SUB(NOW(), INTERVAL 5 MONTH);
END|
DELIMITER ;
Now I have to test it but I could create it so I guess It's fine.
[EDIT]
The event works like a charm.
I have a table with a start_date, end_date and an interval. I would like to update end_date with the value of start_date and the interval.
create table date_test (
start_date date,
end_date date,
date_interval varchar(45)
);
The values I am using for date_interval are like - INTERVAL 1 WEEK, + INTERVAL 1 MONTH.
I would like to do something like:
UPDATE date_test SET end_date = date( concat( start_date, " ", date_interval));
but I get this warning:
1292 Truncated incorrect date value: '2012-01-01 - INTERVAL 1 week'
How can I force this date to get evaluated before updating?
Jonathan Leffler said :
Nearly; there's a crucial difference between the manual page and the question, though. The manual discusses DATE_ADD(date_value, INTERVAL '1' DAY) etc, whereas the question would be having a 'string' value as the second parameter. I fear the question would need a function to convert the string into an INTERVAL type. There doesn't appear to be a 'TO_INTERVAL' function in MySQL.
Here is a function that will take the date as first parameter and the string interval as second parameter.
Simply add the following stored procedure to your database :
CREATE PROCEDURE my_date_add(d DATE, i VARCHAR(50))
BEGIN
DECLARE sign CHAR(1);
DECLARE x INT;
SET sign = SUBSTRING_INDEX(i, ' ', 1);
SET x = SUBSTRING_INDEX(SUBSTRING_INDEX(i, ' ', -2), ' ', 1);
IF sign = '-' THEN
SET x = -x;
END IF;
CASE SUBSTRING_INDEX(i, ' ', -1)
WHEN 'DAY' THEN SELECT DATE_ADD(d, INTERVAL x DAY);
WHEN 'WEEK' THEN SELECT DATE_ADD(d, INTERVAL x WEEK);
WHEN 'MONTH' THEN SELECT DATE_ADD(d, INTERVAL x MONTH);
END CASE;
END
Then you should be able to update your table like this :
UPDATE date_test SET end_date = my_date_add(start_date, date_interval);
What you want to do is :
UPDATE date_test SET end_date = DATE_ADD(start_date, date_interval);
But I'm not sure that using date_interval as the second parameter will work, tell us if it does.
You will find a lot of useful examples in the MySQL documentation, see DATE_ADD() function description.
MySQL does not support values evaluating. So, you cannot use an UPDATE statement directly.
In this case I'd suggest you these ways:
Write a SELECT...INTO OUTFILE statement that would generate a list of UPDATE statemants and output all this statements into the file, then just run this sript.
Or write a stored procedure that would open a cursor on date_test table, in a loop generate and execute UPDATE statements for each record, one by one, using prepared statements.
Ask, if you have a questions about the solutions.