MySQL Custom Order

丶灬走出姿态 提交于 2020-01-13 19:07:19

问题


I have a table that I select data from with a column called parent that's of unsigned integer type.

It has numbers from 0 to 12.

I want to select * from table order by parent asc, but with one exception: place the 0 at the end of the select so it would be like 1,2,3,4,5,6,7,8,9,0.

Is this possible with a single select in MySQL please?


回答1:


I would do something like this:

select * 
from your_table 
order by (parent != 0) desc, parent asc; 



回答2:


select * 
from table 
order by case when parent is 0 then 1 else 0 end, 
    parent asc


来源:https://stackoverflow.com/questions/3601258/mysql-custom-order

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!