Scorpioniz Scorpioniz - 1 month ago 4
MySQL Question

mysql need to get custom order of result

Have MySQL table like

id
,
parent_id
,
order_nr


Now selecting everything like this

SELECT * FROM `table1` WHERE 1 ORDER BY `id` DESC


And getting result like

`id`, `parent_id`, `order_nr`
"27", "22", "3"
"26", "22", "2"
"25", "22", "1"
"24", "0", "0"
"23", "0", "0"
"22", "0", "0"


Need to get same result just in order like this

`id`, `parent_id`, `order_nr`
"24", "0", "0"
"23", "0", "0"
"27", "22", "3"
"26", "22", "2"
"25", "22", "1"
"22", "0", "0"


Or in order like this:

`id`, `parent_id`, `order_nr`
"24", "0", "0"
"23", "0", "0"
"22", "0", "0"
"25", "22", "1"
"26", "22", "2"
"27", "22", "3"

Answer

how about this one:

SELECT * FROM `table1` WHERE 1 ORDER BY order_nr,parent_id,id DESC

you can try to swap what column should be order first.

Comments