Home > OS >  Selecting records from last 24 hours - MySQL
Selecting records from last 24 hours - MySQL

Time:09-30

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:

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;

CodePudding user response:

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;
  • Related