Many-to-many query (chained)

2007-09-29 Thread Rapthor
Hi SQLers, I am searching for an SQL command to combine several many-to-many queries. At the end I only want to get results suitable for all restrictions. Better show you my tables: USER id name 1 frank ROLE id name 1 admin 2 general GROUP id name 1 groupA 2 groupB USER_ROLE

Re: Many-to-many query (chained)

2007-09-29 Thread Peter Brawley
Rapthor, Try ... SELECT u.name FROM user u JOIN user_role ur ON u.id=ur.user_id AND (u.id=1 OR u.id=2) JOIN user_group ug ON u.id=ug.user_id AND (u.id=1 OR u.id=2) HAVING COUNT( DISTINCT ur.role_id ) = 2 AND COUNT( DISTINCT ug.group_id ) = 2; BTW you can't name a table 'group'; it's a

Re: Many-to-many query (chained)

2007-09-29 Thread Rapthor
Great! With a little adaption it works! Yes and of course I have to use another name for groups :) It was just an example. I have slightly different table settings. SELECT u.name FROM user u JOIN user_role ur ON u.id=ur.user_id AND (ur.role_id=1 OR ur.role_id=2) JOIN user_group ug ON