Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Logical where grouping not respected in MySQL

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Could anyone point out why the data is not being returned?

>Solution :

What you’d need is this:

SELECT
  *
FROM
  `table`
WHERE
  (
    (`date` = '2023-05-15' AND `hour` >= '4')
    OR
    (`date` > '2023-05-15')
  )
  AND
  (
    (`date` = '2023-05-16' AND `hour` <= '4')
    OR
    (`date` < '2023-05-16')
  )
ORDER BY
  `id` DESC
LIMIT
  0,1000

Which is why you should never keep the date and time components separately.

It’as like storing 1.25 as…

units tenths hundredths
1 2 5
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading