Josh Fradley Josh Fradley - 4 months ago 12
SQL Question

Mysql not between time and dates

user | completed
mike | 2016-07-10 19:00:00
john | 2016-07-11 08:00:00


I am trying to select all rows in a database where the row completed is NOT between 14:00 the previous day and the current day before 10:00. The script is designed to be run at 10:30 everyday

I've tried this

SELECT name FROM daily_tracking WHERE completed NOT BETWEEN now() - interval 1 day AND NOW() - INTERVAL 8 hour

Answer

you should use date_sub

SELECT name 
FROM daily_tracking 
WHERE completed NOT BETWEEN date_sub(NOW(), interval 1 day ) 
   AND date_sub(NOW(),  INTERVAL 8 hour)