php - Mysql order by column when not empty then order by other column -


i wouls has had me stumpted 2 days. need retirieve data database , order column1 when isn't empty , rest of result column2

column1      column2 1            11 2            12 3            13              14              15              16 

required result

1,2,3,14,15,16 

i've tried numerous approaches, latest failed attempt being

$sql = "select * table order coalesce(column1,column2) desc";

and

`$sql = "select * table order coalesce(column1,column2) asc";

my above sql returning null value column1 above column2

coalesce() work if "empty" values in column1 null. empty strings not trigger coalesce() operation.

beyond that, query not work. cannot select * 2 columns , somehow magically 1 single column in result. you'll need union query:

(select column1 col yourtable)  union  (select column2 col yourtable) order col 

Comments