I have the following table structure/data:
id | date (DATE) | hour (INT) | tally (INT) | sum (INT) |
---|---|---|---|---|
1 | 2023-05-15 | 7 | 12 | 2 |
2 | 2023-05-15 | 7 | 1 | 4 |
With the following query:
SELECT * FROM `table` WHERE
((`date` >= '2023-05-15') AND (`hour` >= '4')) AND ((`date` <= '2023-05-16') AND (`hour` <= '4')) ORDER BY `id` DESC LIMIT 0,1000;
My expectation is that the data in the example is returned. I have two discrete where conditions (parenthesised). Instead when I run the query no data is returned.
Could anyone point out why the data is not being returned?
2
Answers
Your query is the equivalent of this :
Or more precisely this :
Because you have no records with
hour = 4
, the result will be empty.What you’d need is this:
Which is why you should never keep the date and time components separately.
It’as like storing
1.25
as…