Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MYSQL select records older than 1 day

Tags:

sql

mysql

I am trying to select records that are older than 1 day or 24 hours from a MYSQL DB. I can't figure it out why this statement doesn't work with NOW() and it only works if I explicitly add the date manually, here is the code for your review:

date_posted is setup as "datetime" in the DB table.

Works this way (Not desirable, for testing only)

SELECT count( DISTINCT my_id ) AS 'a'
FROM my_table
WHERE date_posted >= DATE_SUB( '2015-10-03 09:10:19', INTERVAL 24 HOUR )  

It should work this way, but it brings a "0" count result:

SELECT count( DISTINCT my_id ) AS 'a'
FROM my_table
WHERE date_posted >= DATE_SUB( NOW(), INTERVAL 24 HOUR ) 
like image 240
Andre Avatar asked Oct 19 '25 23:10

Andre


1 Answers

SELECT count( DISTINCT my_id ) AS 'a'
FROM my_table
WHERE date_posted >= subdate(current_date, 1)

here subdate(current_date, 1) will return your the yesterday date if your want to select only the yesterday post then use the following WHERE date_posted = subdate(current_date, 1)

like image 67
Payer Ahammed Avatar answered Oct 21 '25 14:10

Payer Ahammed



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!