I am trying to do some order in a mysql query that I cannot understand.
id | status | created_at ------------------------ 1 | open | 1348778070 2 | closed | 1348711241 3 | open | 1348839204 4 | closed | 1348738073 5 | banned | 1348238422
How to arrange the above table so that first open records are opened, in ASC order; and then non-open records are second in DESC order? In other words, do you have a dynamic second-level ordering direction based on some condition?
I tried UNION from two SELECT queries with ordering inside them, which does not work, because UNION creates an unordered rowset by default.
I also tried a pseudo-column that subtracts the created_at timestamp from a large number for private status records, so I can just ORDER BY ASC to get the result as below ...
SELECT table.*, (table.created_at) as tmp_order FROM table WHERE table.status = 'open' UNION SELECT table.*, (999999999 - table.created_at) as tmp_order FROM table WHERE table.status = 'closed' ORDER BY tmp_order ASC
It works, but I feel that there should be a better way. Ideally, the solution would not include a random large number, as indicated above
source share