A bit ago ago I posted some optimizer hints and recommended using them to write more readable queries. Today I stumbled across an example of my own work that I changed. This is not quite a case of using BETWEEN instead of AND statements, but it sort of is. Basically, I needed to reuse this query:
SELECT count(*),city,
CASE WHEN lastLogin < now ()-interval 90 day THEN 90
WHEN lastLogin < NOW()-interval 60 day THEN 60
WHEN lastLogin < NOW()-interval 30 day THEN 30
WHEN lastLogin > NOW()-interval 30 day THEN 0
ELSE -1
END
FROM . . .
I wrote this query less than a month ago.
I looked at this query today and wondered, “why did I not put
lastLogin < now ()-interval 60 AND
lastLogin>NOW()-interval 90
in there?” I then realized
what I did.
Because the CASE statement …
[Read more]