MySQL Custom Order

I have a table from which I select data with a column called parent, an unsigned integer type.

It has numbers from 0 to 12.

I want select *from the table in parent asc order, but with one exception: put 0 at the end of the selection so that it is like 1,2,3,4,5,6,7,8,9,0,

Is this possible with one choice in MySQL, please?

+3
source share
2 answers

I would do something like this:

select * 
from your_table 
order by (parent != 0) desc, parent asc; 
+4
source
select * 
from table 
order by case when parent is 0 then 1 else 0 end, 
    parent asc
0
source

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


All Articles