Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL SELECT WHERE with date and time

Tags:

select

mysql

I have a SELECT query where I want to find all rows whose DATE and TIME are between 2011-12-11 23:00:00 and 2011-12-12 23:00:00 I try to do it with WHERE but row is empty

WHERE (date >= '2011-12-11' AND time > '23:00:00' )
AND   (date <  '2011-12-12' AND time < '23:00:00' )

Pls, any good suggestion how to change this?

like image 714
Andrew Avatar asked Oct 31 '25 20:10

Andrew


1 Answers

You could use:

SELECT * FROM table WHERE DATETIME(date) BETWEEN '2011-11-11 23:00:00' AND '2011-12-13 23:00:00'

or separate:

SELECT * FROM table WHERE DATETIME(date) > '2011-12-11 23:00:00' AND DATETIME(date) < '2011-12-13 23:00:00'


EDIT:

I am not sure I understand what you are trying to achieve here or how your DB is laid out but assuming date and time are separate fields:

SELECT * FROM table WHERE DATETIME(concat(DATE(date),' ',TIME(time))) BETWEEN '2011-11-11 23:00:00' AND '2011-12-13 23:00:00'

I haven't tested but this may work.

like image 86
JM4 Avatar answered Nov 02 '25 22:11

JM4