How to remove mysql records from half an hour (or more) ago

Asked

Viewed 602 times

2

I need to remove mysql records that were made half an hour (or more) ago. That is, leave only the last 30 minute records. This every time a certain php script runs. How do I?

  • you have a field with the time in the record? if you have just make the comparison with the current time.

  • These MYSQL date functions will help you: https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html

2 answers

4


DELETE FROM nometabela 
WHERE campodatahorainsercao < DATE_SUB(NOW(), INTERVAL 30 MINUTE)

Notice the following thing:

All that has in the table that was inserted more than 30 minutes will be deleted.

All records below the date of the time of execution - 30 minutes will be deleted. That is, only the most recent who have not completed 30 minutes yet.

0

Well, since there are not many details I will imagine that you have a table of users, in this table has constant Inserts all day.

Ai in user you would have, login, password, registered

All you have to do is delete based on the registered dateIn, passing as parameter an object Date of half an hour before

Assuming it’s 20/01/2017 20:00:00 sharp

You run the script in PHP

then just do

delete from Where registered user No > 20/01/2017 19:30:00

Based on the current time it is 20/01/2017 20:00:00 on the dot I sent as parameter half an hour before it is 20/01/2017 19:30:00

Delete for min all "registered" records above 20/01/2017 19:30:00

All "registered" users who are greater than 01/20/2017 19:30:00 will be deleted. In other words everything recorded in the 30-minute period will be deleted.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.