I have this table in my database:
table t
id | a | b | date_x
1 | 81 | 12 | 2018-03-16
2 | 9 | 54 | 2025-04-21
3 | 81 | 67 | 2018-03-16
4 | 763 | 81 | 2018-03-16
5 | 90 | 22 | 2025-12-08
date_x is type DATE
I would like to select the rows where a = 81 or b = 81 and date_x is before 2019-05-28 .
So I perform the following query in MySQL Workbench:
SELECT * FROM t
WHERE a = '81' OR b = '81'
AND date_x > '2019-05-28';
This is what I get:
1 | 81 | 12 | 2018-03-16
3 | 81 | 67 | 2018-03-16
I would expect that 2018-03-16 is not later than 2019-05-28 . Moreover, why only 2 rows returned? There is another with same date in date_x column.
This query return the same:
SELECT * FROM t
WHERE a = '81' OR b = '81'
AND date_x > str_to_date('2019-05-28', '%Y-$m-%d');
I have performed the following query for debug:
SELECT * FROM t
WHERE a = '81' OR b = '81'
AND date_x < '2019-05-28';
and
SELECT * FROM t
WHERE a = '81' OR b = '81'
AND date_x < str_to_date('2019-05-28', '%Y-$m-%d');
then both return, as expected:
1 | 81 | 12 | 2018-03-16
3 | 81 | 67 | 2018-03-16
4 | 763 | 81 | 2018-03-16
I read the following Q/A but I'm still missing something:
- MySQL date comparison function
- MySQL date comparison issue
- MySQL date/author comparison
- MySQL Date comparison advice
- MySQL date comparison
Any hint? Thank you