0

I need to get some records from a mysql database and order them in a special order: some records have a special string and some don't. I need to put the ones who don't have this special string and than the ones who have. Is there a way using mysql like

SELECT * FROM mutable ORDER BY col_name, "special string not exists"

or do i have t odo it in php after getting the records without order?

pindol
  • 2,110
  • 6
  • 35
  • 52

1 Answers1

1

With decode, you can convert one value to another.

SELECT * FROM mutable ORDER BY col_name, decode(column_with_sepcial_string, "special string", 1, 0)

Those who have the special string get a 1 as value, those who don't get a 0 (do note that null values always get null back). When you order 0 and 1 ascending, the 0's (those who don't have the special string) are above the ones with a 1 (with the special string)