MySQL order over multiple statements

I have a situation where I need to sort my records by their "status", which consists of a combination of fields. Here is an example of how it should return results sorted by status in ascending order:

| Sent Received Approved -------------------------------------------------- record1 | null null null record2 | 2012-01-01 null null record3 | 2012-01-01 2012-01-01 null record4 | 2012-01-01 2012-01-01 2012-01-01 

How do I create a MySQL query that will order these records by their common "status"?

+6
source share
1 answer
 order by case when sent is null and received is null and approved is null then 1 when received is null and approved is null then 2 when approved is null then 3 else 4 end 
+11
source

Source: https://habr.com/ru/post/909468/


All Articles