MYSQL output in tree format OR Adding level (Parent-Child)

后端 未结 4 1870
失恋的感觉
失恋的感觉 2020-12-19 21:25

Below is what I have in my table.

myTable

++++++++++++++++++++
Parent   +  Child
++++++++++++++++++++
  C1     +    G1
  C1     +    G2
  C1     +          


        
4条回答
  •  遥遥无期
    2020-12-19 22:08

    If you restructured your table a bit you could use something like:

      SELECT Child,CONCAT(LPAD('',Clevel,' '),Child),etc from tablename
    

    The restructuring is that you would need the root node in as a row with parent node of 0. You can add your own ordering with parent / child / C level to get the sequence as desired.

    I know this is from a few years back, but it might save someone else some effort!

提交回复
热议问题