Query 1:
SELECT *
FROM user_d1
WHERE EXISTS (SELECT 1
FROM `user_d1`
WHERE birthdate BETWEEN '1989-08-04' AND '1991-08-04')
ORDER BY timestamp_lastonline DESC
LIMIT 20
Query 2:
SELECT *
FROM user_d1
WHERE birthdate BETWEEN '1989-08-04' AND '1991-08-04'
ORDER BY timestamp_lastonline DESC
LIMIT 20
And what I really don’t understand: why does Query 2 return the wrong results? It returns a list ordered first by birthdate and then by timestamp_lastonline…
Query 1 : If at least one record between the dates exists then the entire talbe is retrieved.
Query 2 : Only records between the dates are retrieved.
Read here for how
EXISTSworks.