Robert E. McIntosh
Robert E. McIntosh

Reputation: 6135

MySQL order by 0 then largest

I am trying to do a mysql sort that displays 0 first and then by 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 this

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 with 0 at the beginning?

Upvotes: 4

Views: 5350

Answers (2)

Thanos Darkadakis
Thanos Darkadakis

Reputation: 1729

you have to use 2 filters

select * from mytable
order by mycolumn=0 desc, mycolumn desc

Upvotes: 3

Gordon Linoff
Gordon Linoff

Reputation: 1270503

Try this order by statement:

order by val = 0 desc, val desc

The first part is a boolean that evaluates to "1" when the value is 1 and otherwise 0. The second orders the rest of the values in descending order.

Upvotes: 11

Related Questions