I have this query:
SELECT user.user_id,
user.user_name,
user.user_hash,
suspension.suspension_expireson
FROM user
LEFT JOIN suspension
ON suspension.suspension_user = user.user_id
WHERE user.user_name = "tester"
ORDER BY suspension.suspension_expireson DESC
LIMIT 1;
It orders the suspension_expireson so that the highest number (it’s unixtime, so the furthest in the future) comes up first, but I want NULL to be ranked higher than any number. So if there’s a row with the value NULL in suspension_expireson, I want that one.
How should I modify the query to do that?
Thanks in advance!
Use a
CASEstatement in theORDER BYto give a lower constant value to nulls 0 than other rows, which get a 1. The 0 sorts ahead of the 1, then the rest (1) are subordered bysuspension_expireson DESC.