Treeview from sql table

后端 未结 2 653
忘掉有多难
忘掉有多难 2021-01-12 08:29

I have sql table like below. I have to show it in tree view

id   parentid     name
1     NULL       outlook
2     1      overcast
3     1       rainy
4     1         


        
2条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-12 09:03

    WITH    q AS 
            (
            SELECT  *
            FROM    mytable
            WHERE   ParentID IS NULL -- this condition defines the ultimate ancestors in your chain, change it as appropriate
            UNION ALL
            SELECT  m.*
            FROM    mytable m
            JOIN    q
            ON      m.parentID = q.ID
            )
    SELECT  *
    FROM    q
    

提交回复
热议问题