简体   繁体   中英

MySQL Query to delete rows whose timestamp is older than current timestamp

I am looking for a query that is able to delete all rows from a table in a database where timestamp is older than the current date/time or current timestamp.

Would really appreciate some help out here urgently!

Here's the query I am using but as I thought it ain't working:

delete from events where timestamp<CURRENT_TIMESTAMP{);

Um... This may seem silly, but every record in the table will be older than Now(), since Now() is calculated at the time that query is processed. If you you want to delete a record that's older than another record, then you don't want to use Now(), but the timestamp from the record you're comparing the rest to. Or, if you want to delete records that are older than a specific point in time, then you need to calculate the timestamp that you want to use to compare against. For example, to delete records older than 10 minutes, you could use this:

DELETE FROM events WHERE timestamp < (NOW() - INTERVAL 10 MINUTE)

Or, for deleting records that are over a day old:

DELETE FROM events WHERE timestamp < (NOW() - INTERVAL 1 DAY)

For specific points in time (eg Oct. 12th, 2012 at 4:15:00 PM GMT), there's a method to do that, but the syntax escapes me, right now. Where's my MySQL manual? :)

delete from events where timestamp < NOW()

应该足够了。

DELETE FROM events WHERE timestamp < UNIX_TIMESTAMP(NOW())

或者如果是标准日期时间

DELETE FROM events WHERE timestamp < NOW()

Hibernate (hql) Delete records older than 7 days

I am not sure, but you can Try this:

    String hqlQuery = "from PasswordHistory pwh "
          + "where pwh.created_date < datediff(curdate(), INTERVAL 7 DAY)";

            List<Long> userList = (List<Long>)find(hqlQuery);
    deleteAll(userList );// from baseDao

public void deleteAll(Collection list) {
        getHibernateTemplate().deleteAll(list);
    }
DELETE FROM table WHERE date < '2011-09-21 08:21:22';

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