MySQL order at 0, then the largest

I am trying to do a mysql sort that displays 0 first and then the largest number.

My current mysql statement returns

 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 0, 0, 0 

But I would like to get it

 0, 0, 0, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 

Is it possible to build a MySQL query that orders an integer from largest to smallest using 0 at the beginning?

+4
source share
2 answers

Try this order by statement:

 order by val = 0 desc, val desc 

The first part is a logical value that is calculated to "1" when the value is 1 and otherwise 0. The second order of the remaining values ​​is in descending order.

+9
source

you need to use 2 filters

 select * from mytable order by mycolumn=0 desc, mycolumn desc 
+2
source

All Articles