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**))
Related
I'm trying to write mysql query to delete records older than 24 hours.
The SELECT sql statement which i used is below
SELECT * FROM Request WHERE
timeStamp <= UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 1 DAY))
Table contains lot of records older than 1 day but the result of this sql query is empty. Also it doesn't show any error message.
Timestamp field structure is
Name: timeSatamp
Type: timestamp
Default: CURRENT_TIMESTAMP
Can somebody help me to find out the mistake in this statement?
Thanks in advance!
You dont need the FROM_UNIXTIME() so this will do what you want
SELECT * FROM `ts` WHERE timeStamp <= DATE_SUB(NOW(), INTERVAL 1 DAY)
Or
SELECT * FROM `ts` WHERE timeStamp <= NOW() - INTERVAL 1 DAY
you can use DATEDIFF instead of the comparing from two date.
SELECT * FROM Request WHERE DATEDIFF(timestamp, NOW())>=1;
DATEDIFF returns a number of days between two date/timestamp.
https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_datediff
Your Query should be like this:
SELECT * from Request where FROM_UNIXTIME(timeStamp) <= (NOW() - INTERVAL 1 DAY);
If the field timestamp contains 10 digits (like "1566836368") the right answer is
SELECT * from Request where FROM_UNIXTIME(timeStamp) <= (NOW() - INTERVAL 1 DAY);
like previously suggested by #akshaypjoshi
Delete 24 Hours older records using Mysql
DELETE FROM `table_name` WHERE `dateCreate` < (Now() - INTERVAL 24 HOUR);
I have a table 'schedule' and column 'travel_date'.
travel_date is having 'a predefined date' in that.
I want to alter that column with '5days' more.
like
UPDATE Schedule SET travel_date=''+5days ;
I used
UPDATE schedule SET travel_date = (travel_date+5);
It worked how ?
In MySQL you can do that with
UPDATE customer
SET register_date = DATE_ADD(register_date, INTERVAL 5 DAY)
Why would you want to add 5 days to every customers register date???
Are you sure this is what you want to do?
UPDATE customer SET [register_date] = DATE_ADD([register_date], INTERVAL 5 DAY)
If it is a datetime column, use the DATE_ADD() function:
UPDATE customer SET register_date = DATE_ADD(register_date, INTERVAL 5 DAY)
Using DATE_ADD()
You can use the DATE_ADD() function to handle adding a given interval (e.g. days, minutes, hours, etc.) to an existing date column:
UPDATE customer
SET register_date = DATE_ADD(register_date, INTERVAL 5 DAY)
Using Date Arithmetic
Alternatively, you can simply use date arithmetic as well, which is similar to your previous example:
UPDATE customer
SET register_date = register_date + INTERVAL 5 DAY
Add date in timestamp
I want to run below query
UPDATE table
SET columnA = '2017-03-21 23:57:19'
WHERE .....
However, I want to construct the timestamp by taking whatever yesterday's date is, at 23:57:19 hours.
The datatype for columnA is timestamp. 2017-03-21 is yesterday's date. Am I supposed to use subdate(CURDATE(),1)?
UPDATE table
SET columnA = 'subdate(CURDATE(),1) 23:57:19' WHERE ..... ?
Or I have another way to do this ?
Use DATE_SUB()
UPDATE table
SET columnA = DATE_SUB('2017-03-22 23:57:19', INTERVAL 1 DAY)
or
UPDATE table
SET columnA = DATE_SUB(NOW(), INTERVAL 1 DAY)
If you wanted to get yesterday at 23:57:19 then try this:
DATE_ADD(TIMESTAMP(DATE_SUB(CURDATE(), INTERVAL 1 DAY)), INTERVAL '23:57:19' HOUR_SECOND)
DATE_SUB(CURDATE(), INTERVAL 1 DAY) - yesterday's date
TIMESTAMP(...) - resets yesterday to midnight
DATE_ADD(..., INTERVAL '23:57:19') - adds 23:57:19 to yesterday from midnight
Demo
I am trying to create a query that will limit insertion into a table based on the last time the poster sent data to the table.
For example if you posted data to the table then you are locked out of the system for another 10 hours. Here is what I came up with so far. But I get nowhere with the actual results on the data. Any help?
SELECT DATE( `date` )
FROM tablename
WHERE DATE( CURDATE( ) ) < CURDATE( ) - INTERVAL 1002
DAY
LIMIT 0 , 30
This will return a single post from the last 10 hours, if it exists:
SELECT *
FROM tablename
WHERE `date` >= NOW() - INTERVAL 10 HOUR
LIMIT 1
I'm assuming date is declared as DATETIME, since actual DATE does not contain the time part and hence is only day-accurate.
If date is an integer UNIX timestamp, use this:
SELECT *
FROM tablename
WHERE `date` >= UNIX_TIMESTAMP(NOW() - INTERVAL 10 HOUR)
LIMIT 1
There are a number of ways you could do this. Perhaps if you have a user settings table you could simply add a "last_insert" field, and store the timestamp as an integer value- that would be a super simple way to do it- you could check the current timestamp vs user_settings.last_insert and voila!
I suppose you could use datetime too. Whatever floats the boat.
First of all, you need a DATETIME column and not a DATE column. Assuming that tablename.date is a DATETIME column, then 10 hours before right now is CURRENT_TIMESTAMP - INTERVAL 10 HOUR.
First of all create a Time (TIMESTAMP DEFAULT CURRENT_TIMESTAMP) columnt in your table. It will be automatically set to current date on row insert
Then check:
SELECT COUNT(*) FROM Table WHERE Time > NOW() - INTERVAL 10 HOUR
If its 1 or more - block
You must compare the time last post was put with current time, not current time with current time :|
I'm trying to offset a timezone error from PHP. All times recorded in table 'test' was ahead by two hours. What I want is to update each record by minusing two hours from the time that is already there.
I tried:
UPDATE test
SET LastModifiedDate = SUBTIME( LastModifiedDate, '02:00:00' )
But this just updates all fields with the same value.
Please assist
tthanks
update test set LastModifiedDate = LastModifiedDate - interval 2 hour;
Use the DATE_SUB() function:
UPDATE test SET LastModifiedDate = DATE_SUB(LastModifiedDate, INTERVAL 2 HOUR)
Test it first to be certain it's doing what you want:
SELECT LastModifiedDate, DATE_SUB(LastModifiedDate, INTERVAL 2 HOUR) FROM test;
update test set LastModifiedDate = adddate(LastModifiedDate, interval -2 hour);
this will modify all your dates to -2 hour. you can narrow down the result in "where" section of the query by targeting specific rows.