Cainã
Cainã

Reputation: 955

How to DELETE all entries that aren't in a time range in MySQL?

I have a table for connections that have the following structure:

->ip
->first (datetime of the entry creation)
->last (datetime of last update to the entry)

Datetimes are in the format Y-m-d H:i:s.

Now I want to delete every entry that have last - first > 5 minutes as true. How can I do that in the SQL query?

Upvotes: 0

Views: 62

Answers (2)

Luca Rainone
Luca Rainone

Reputation: 16468

use TIMESTAMPDIFF

DELETE FROM `tableName` WHERE TIMESTAMPDIFF(MINUTE,`first`,`last`) > 5

Upvotes: 1

Andreas Linden
Andreas Linden

Reputation: 12721

simply add it as where condition

DELETE FROM `table` WHERE `last` - `first` > 5;

Upvotes: 3

Related Questions