MySQL Case statement within order by

Basically I am trying to write a case statement within the ORDER BY clause which says

if delay = 1, then order by m.delay DESC, p.last_online ASC, m.state DESC, m.priority DESC
else order by m.state DESC, m.priority DESC

Any ideas how I do this?

Actually I think I’ve half sussed it…

ORDER BY m.delay DESC, CASE WHEN m.delay = 1 THEN p.last_online END, m.state DESC, m.priority DESC

How do I make p.last_online DESC?

like this –

ORDER BY m.delay DESC , CASE WHEN m.delay = 1 THEN p.last_online ELSE NULL END DESC , m.state DESC , m.priority DESC

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.