0

I need to get both group names as a result, although I have no idea how to do this query.

Table 1 (groups)

+---------+--------+
| id      | name   |
+---------+--------+
| 1       | newb   |
| 2       | user   |
| 3       | editor |
| 4       | boss   |
+---------+--------+

Table 2 (inheritance)

gid = group id; pid = parent id

+---------+--------+
| gid     | pid    |
+---------+--------+
| 3       | 1      |
| 3       | 2      |
| 4       | 1      |
| 4       | 2      |
| 4       | 3      |
+---------+--------+

Expected Results

Now I want the names of the groups as a result, not the ids, so I need to get this result:

+---------+--------+
| gname   | pname  |
+---------+--------+
| editor  | newb   |
| editor  | user   |
| boss    | newb   |
| boss    | user   |
| boss    | editor |
+---------+--------+

Thanks!

1 Answer 1

0

You need to use 2 copies of names table:

SELECT groups_1.name AS gname, groups_2.name AS pname
FROM inheritance
JOIN groups AS groups_1 ON inheritance.gid = groups_1.id
JOIN groups AS groups_2 ON inheritance.pid = groups_2.id

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.