Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

SQL Select data in the last 1 minutes

i am currently practicing my SQL skills. I wanted to get all data in the past 1 minutes.

The query is SELECT * FROM menfesses WHERE created_at >= NOW() - INTERVAL 1 MINUTE;
But somehow, it returns all data.

I have also use date_add approach and nothing works

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

What did i do wrong? Thanks

Query Result

>Solution :

Probably your server time that’s not what you think it is.

This work with a 5 min laps.

SQL Fiddle

MySQL 5.6 Schema Setup:

CREATE TABLE t1
    (`c_date` datetime)
;
    
INSERT INTO t1
    (`c_date`)
VALUES
    (NOW() - INTERVAL 30 MINUTE),
    (NOW() - INTERVAL 2 MINUTE),
    (NOW() - INTERVAL 1 MINUTE)
;

Query 1:

SELECT *,NOW() FROM t1 WHERE c_date >= NOW() - INTERVAL 5 MINUTE

Results:

|               c_date |                NOW() |
|----------------------|----------------------|
| 2023-02-09T15:44:05Z | 2023-02-09T15:46:20Z |
| 2023-02-09T15:45:05Z | 2023-02-09T15:46:20Z |
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading