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

前端 未结 7 1990
生来不讨喜
生来不讨喜 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
    

提交回复
热议问题