MySQL – Query Records Between Yesterday and Today with Specific Time

dateMySQL

I seem to be having a case of the Fridays. I am new to MySQL (few weeks now) and I just can't seem to get a report automated. I need to be able to query data from yesterday 8 am until today 8 am. I am going to schedule the report to run at 10 am. What is the best way to do this? I have been running it manually with BETWEEN but this will not do. I tried to use the interval feature but no luck. I think I am almost there.

One try:

purchase_date BETWEEN '2020-11-18 09:00:00' and (DATE(NOW()) - INTERVAL 16 hour)

Another try:

purchase_date BETWEEN BETWEEN (DATE(NOW()) - INTERVAL 40 hour) and (DATE(NOW()) - INTERVAL 16 hour)

If you can just point me to the function or best option in this case, I should be able to figure out the arguments. Ideas/suggestions/thoughts? Thank you!

Best Answer

USE CURDATE() and INTERVAL

SELECT CURDATE() - INTERVAL 1 DAY + INTERVAL 8 HOUR, CURDATE() + INTERVAL 8 HOUR

RESULT

# CURDATE() - INTERVAL 1 DAY + INTERVAL 8 HOUR  CURDATE() + INTERVAL 8 HOUR
2020-11-19 08:00:00                             2020-11-20 08:00:00
Related Question