Reputation:
I have 1 table called Location, containing columns LocationID, ParentLocationID and LocationName. I want to display a table with "Location" and "Parent Location" both derived from the LocationID. How might I do this ?
Example:
Say for example my table looked like this:
LocationID | ParentLocationID | LocationName
1 | 3 | abc
2 | 3 | def
3 | NULL | xyz
I want my returned table to look like:
LocationName | ParentLocationName
abc | xyz
def | xyz
Upvotes: 0
Views: 261
Reputation: 6418
Do a self join. For example:
SELECT a.LocationID, a.LocationName,
parent.LocationID, parent.LocationName
FROM Location a
INNER JOIN Location parent ON a.ParentLocationID=parent.LocationID;
Upvotes: 1