edmund
edmund

Reputation: 31

SQL, multiple sorting order

I would like to ask if i can achieve a query with one "order by" without using union:

first_order = column1 != column2;
seconder_order = column1 = column2;
third_order = column is null;

ex. select * from table_A order by first_order , seconder_order , third_order

But when i try this its not working.

What im trying to do is to display on my table with this list first_order then second_order and last the third_order. Im no expert on sql. Hope you can help me on this.. Thanks in advance!

Upvotes: 1

Views: 42

Answers (1)

turutosiya
turutosiya

Reputation: 1041

If you're using MySQL, try :

ORDER BY (CASE
    WHEN column1 != column2 THEN 0
    WHEN column1  = column2 THEN 1
    WHEN column is null     THEN 2
    ELSE 3
END) ASC;

Upvotes: 2

Related Questions