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

Selecting records from last 24 hours – MySQL

I’m trying to run this MySQL command to select records with a lead_submitted date in the last 24 hours.

The issue is it grabs future dates too (fyi I’m delaying some leads so basically set a date in the future for these to be processed).

For example, if I run the command:

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

SELECT id, lead_submitted, processed FROM leads WHERE (lead_submitted > now() - INTERVAL 24 HOUR) ORDER BY id DESC;

I get this result:

10 | 2022-10-04 13:24:13 | N

~

Why is this? I just want to select records from the last 24h.

FTR, when I run SELECT NOW() as now; I get 2022-09-30 14:00:12

Any help would be greatly appreciated.


EDIT:
I’ve found this works as a workaround, but it feels nasty:

SELECT id, lead_submitted, processed FROM leads WHERE (lead_submitted > now() - INTERVAL 24 HOUR) AND (lead_submitted < now() + INTERVAL 1 SECOND) AND processed = 'N' ORDER BY id DESC LIMIT 1;

>Solution :

You probably want to use:

SELECT id, lead_submitted, processed 
FROM leads 
WHERE lead_submitted between (now() - INTERVAL 24 HOUR) and now() 
ORDER BY id DESC;
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