Selecting records in order of parent id

前端 未结 4 849
天涯浪人
天涯浪人 2020-12-31 17:14

Simple question.. just can\'t get the result set in the order I need :p

I have a table \"categories\"

id    | name     | parent
1       apple      0
         


        
4条回答
  •  别那么骄傲
    2020-12-31 17:49

    This would work, but not recursively.

    SELECT 
      b.* 
    FROM
      categories a 
      RIGHT JOIN categories b ON b.parent = a.id
    ORDER BY
      COALESCE(a.name, b.name), b.name
    

提交回复
热议问题