Reputation: 15
I have a table group
containing groups with their parent group:
groupid name parentid
-------------------------
1 test 1
2 second 1
3 3rd 1
4 next 2
How can I query this table to receive a result like this (name instead id)
groupid name parent
---------------------------
1 test test
2 second test
3 3rd test
4 next second
Upvotes: 0
Views: 173
Reputation: 216
You can use below Query:
select A.groupid, A.name, B.name as parentid
from group A
left join group B on A.groupid = B.parentid
Upvotes: 0
Reputation: 222462
That's a self-join:
select t.groupid, t.name, p.name parentname
from mytable t
inner join mytable p on p.groupid = t.parentid
If you have missing parentid
s, then use a left join
instead.
Upvotes: 2