0

We have following table with columns: CategoryId | CategoryName with CategoryId as primary-key.

We have following data:

0   Other
1   Bumper
2   Door
3   Roof
4   Fender

I use select * from these table and i get rows in the above sequence only i.e. 0 first and 4 at last.

Is there any way I can get 0 at last? i.e. 1,2,3,4,0?

Sahil Sharma
  • 3,847
  • 6
  • 48
  • 98

1 Answers1

2
select * from yourtable order by CategoryId=0,CategoryId

You can use arbitrary expressions in an order by clause. By first ordering by CategoryId=0, you get all records for which that is false, followed by records (obviously only one, since it is the primary key) for which it is true. Then each of those sets of records are sorted by CategoryId.

ysth
  • 96,171
  • 6
  • 121
  • 214