MySql: ORDER BY parent and child

前端 未结 3 496
情话喂你
情话喂你 2020-12-23 17:53

I have a table like:

+------+---------+-
| id   | parent  |
+------+---------+
| 2043 |    NULL |
| 2044 |    2043 |
| 2045 |    2043 |
| 2049 |    2043 |
|          


        
3条回答
  •  情歌与酒
    2020-12-23 18:17

    The solution above didn't work for me, my table used 0 instead of NULL. I found this other solution: you create a column with the concatened parent id and child id in your query and you can sort the result by it .

    SELECT CONCAT(IF(parent = 0,'',CONCAT('/',parent)),'/',id) AS gen_order
    FROM table 
    ORDER BY gen_order
    

提交回复
热议问题