我有一个名为Location的表,包含列LocationID,ParentLocationID和LocationName。我想显示一个表格,其中“Location”和“Parent Location”都是从LocationID派生的。我怎么能这样做?
示例:
比如说我的表看起来像这样:
LocationID | ParentLocationID | LOCATIONNAME
1 | 3 | abc
2 | 3 | def
3 | NULL | xyz
我希望我的返回表格如下:
LocationName | ParentLocationName
abc | xyz
def | xyz
答案 0 :(得分:1)
自我加入。例如:
SELECT a.LocationID, a.LocationName,
parent.LocationID, parent.LocationName
FROM Location a
INNER JOIN Location parent ON a.ParentLocationID=parent.LocationID;