Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter query by CURDATE()

Tags:

sql

php

mysql

SELECT *
FROM `summary`
WHERE submit_date = curdate()
AND submit_date > '06:00'
AND submit_date < '21:00'
LIMIT 0 , 30

This is pulling all dates including today? submit_date is a column that shows has DATETIME and it's not filtering them out like they should be. What am I missing? Thanks!

like image 591
Mike Avatar asked Mar 11 '26 05:03

Mike


1 Answers

Your 2 limiting conditions should be comparing against the value returned by TIME() if it is a DATETIME column, and the first condition should be limited to the DATE() portion only.

SELECT *
FROM `summary`
WHERE
  /* Truncate to the date only to compare against CURDATE() */
  DATE(submit_date) = curdate()
  /* And truncate to the TIME() to compare against these time literals. */
  /* note also that the time literals may need seconds as well */
  AND TIME(submit_date) > '06:00:00'
  AND TIME(submit_date) < '21:00:00'
LIMIT 0 , 30
like image 156
Michael Berkowski Avatar answered Mar 12 '26 19:03

Michael Berkowski



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!