简体   繁体   中英

Delete records older than 24 hours or 1 day based on timestamp

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

你的查询应该是这样的:

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

使用 Mysql 删除 24 小时旧记录

DELETE FROM `table_name` WHERE `dateCreate` < (Now() - INTERVAL 24 HOUR);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM