Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WHERE datetime older than some time (eg. 15 minutes)

Tags:

mysql

I was looking at:

MySQL Select rows where timestamp column between now and 10 minutes ago

I have a col named creation_date holding a datetime stamp: 2013-09-10 11:06:42

I would like to pull all records that are OLDER than 15 minutes using:

WHERE creation_date >= DATE_SUB(NOW(),INTERVAL 15 MINUTE)

However the query always returns 0 results, even with items older then 15 minutes present in the database.

like image 965
Hydra IO Avatar asked Sep 10 '13 19:09

Hydra IO


People also ask

How do you select all records that are 10 minutes within a TIMESTAMP in MySQL?

SELECT col1, col2, col3 FROM table WHERE DATE_ADD(last_seen, INTERVAL 10 MINUTE) >= NOW();

What is the difference between datetime and TIMESTAMP?

The DATETIME type is used for values that contain both date and time parts. MySQL retrieves and displays DATETIME values in ' YYYY-MM-DD hh:mm:ss ' format. The supported range is '1000-01-01 00:00:00' to '9999-12-31 23:59:59' . The TIMESTAMP data type is used for values that contain both date and time parts.

What is the difference between date and datetime?

The DATE type is used for values with a date part but no time part. MySQL retrieves and displays DATE values in ' YYYY-MM-DD ' format. The supported range is '1000-01-01' to '9999-12-31' . The DATETIME type is used for values that contain both date and time parts.

Which is better datetime or TIMESTAMP?

TIMESTAMP is four bytes vs eight bytes for DATETIME . Timestamps are also lighter on the database and indexed faster. The DATETIME type is used when you need values that contain both date and time information. MySQL retrieves and displays DATETIME values in YYYY-MM-DD HH:MM:SS format.


2 Answers

older would be WHERE creation_date < DATE_SUB(NOW(),INTERVAL 15 MINUTE)

like image 118
dognose Avatar answered Oct 11 '22 05:10

dognose


There are many ways to achieve this with INTERVAL. I will present three:

first:

WHERE creation_date < DATE_SUB( NOW(), INTERVAL 15 MINUTE ) 

second:

WHERE creation_date < NOW() - INTERVAL 15 MINUTE 

third:

WHERE creation_date < NOW() + INTERVAL -15 MINUTE 

Inspirtion

like image 21
simhumileco Avatar answered Oct 11 '22 04:10

simhumileco