Display Parent-Child relationship when Parent and Child are stored in same table

前端 未结 7 1852
生来不讨喜
生来不讨喜 2020-12-09 19:03

I have SQL Server table structure like below:

ID    Name     ParentID
-----------------------
1     Root       NULL
2     Business   1
3     Finance    1
4           


        
相关标签:
7条回答
  • 2020-12-09 19:56

    This is a simple way:

    SELECT ID,Name,(SELECT TOP 1 Name FROM Table t WHERE t.ParentID=ParentID) AS ParentName FROM Table
    
    0 讨论(0)
提交回复
热议问题