Gediminas Gediminas - 16 days ago 5
MySQL Question

Order by own `position` using sql

I have few hundreds items in table with

position is null
, I want to do an order by position and set
position
to
1,2,3,4
and etc, but also live the other results with same
position is null


the problem is, if I'm ordering
by order by position asc
, first results come where
position is null
and only after them comes results I want to be first. I what that these results with
null
would go in the end.

Is it possible to solve this only with sql?

Answer
select position
from YourTable
order by case 
when position is null 
then 1 else 0 end, position