Question

I want to join a table which contains n:m relationship between groups. (Groups are defined in a separate table). This table only has entries listing a member_group_id and a parent_group_id.

Given this structure:

id(int) | member_group_id(int) | parent_group_id(int)

The "base" query looks like this:

select p1.group_id, p2.group_id, p1.member_group_id, p2.member_group_id
from group_member_group as p1 
join group_member_group as p2 
on p2.member_group_id = p1.member_group_id

The "base" query correctly shows all relationships (I checked by doing it manually.)

The problem is when I try to apply a where clause to this query to filter for a specific group as "point of origin" (the first group for which I want all parent groups) it returns only the closest parents. For example like this:

select p1.group_id, p2.group_id, p1.member_group_id, p2.member_group_id
from group_member_group as p1 
join group_member_group as p2 
on p2.member_group_id = p1.member_group_id
where p1.group_id = 1

Can anyone give a clue how I can fix this? Or a different approach to realize this. (I suppose I could always do this in my C++ source code on the server side but I would have to transfer a entire table which has a high growth potential to the application server.)

UPDATE:

select p1.group_id, p2.group_id, p1.member_group_id, p2.member_group_id
from group_member_group as p1 
join group_member_group as p2 
on p2.group_id = p1.member_group_id

Typing mistake confirmed. Now I don't get past first level of inheritance period. Thanks at denied for pointing that out.

UPDATE2: Expected Result

id | group_id | member_group_id
--------------------------------
1  |    1     |    2
2  |    2     |    3
3  |    3     |    4
4  |    4     |    5
5  |    5     |    6
6  |    6     |    7

expected result:

ids

2
3
4
5
6
7
Was it helpful?

Solution

on p2.member_group_id = p1.member_group_id

it looks like mistake.

Maybe you wanted to type

on p2.parent_group_id = p1.member_group_id

Maybe you should look for Nested Set Model for parent-child relationships.

It's much easier to make queries like this in that model.

OTHER TIPS

I "developed" an other "solution" to this if you wanna call it that since it technically is not a single query anymore.

I wrote a MySQL procedure which does exactly what I want. It searches for all parents to a single node, then for the parents of the next level and so on and checks if new results are found (need to detect loops in case they happen, which should be prevented by application logic but to be on the save side I also restricted the max distance to a given amount).

But it really is a pain in the ass and has to be modified for every table it needs to be used on.

I guess I will stick with a tree model and just put "accounts" if you wanna call them that in multiple groups. Meaning a account has a 1:n relationship with groups and a group has a n:1 relationship to parents (n groups have 1 parent, that parent may have an other parent up until the "root" node (classic tree)).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top