Hello,
first, here the description of what I want to do:
There are users and roles. Users can have many roles. This relation is stored in a separate table. I want to create a search form where you can search for users with specified roles as an AND connection. Eg, you select 3 roles, the result should just be useres that have all three roles at the same time.
My tables with excerpt sampledata:
user_users
---------------------------------------
id username isDeleted
.......................................................
1 admin 0
11 test4 0
2 Guest 0
10 test2 0
12 Test5 0
13 Test6 0
14 Test7 0
15 Test8 0
16 Test9 0
17 Test 10 0
18 Dummy 0
roles
---------------------------------------
id roleName
.......................................................
1 Administrator
2 Guest
3 Member
4 Moderator
7 Another 1
8 Another 2
9 Another 3
acl_usersroles
---------------------------------------
user_users_id acl_roles_id isDeleted
.......................................................
12 2 0
1 8 0
11 2 0
11 3 0
15 2 0
14 4 0
14 8 0
14 9 0
14 2 0
16 3 0
16 4 0
16 8 0
16 9 0
16 2 0
17 4 0
17 7 0
17 2 0
1 2 0
My SQL query till now is
SELECT `user_users`.`id`, `user_users`.`username`
FROM (`user_users`)
JOIN `acl_usersroles` ON `acl_usersroles`.`user_users_id`=`user_users_id`
WHERE `acl_usersroles`.`acl_roles_id` = 8
AND `acl_usersroles`.`acl_roles_id` = 7
AND `user_users`.`id` = `acl_usersroles`.`user_users_id`
AND `user_users`.`isDeleted` = 0
GROUP BY `user_users`.`username`
ORDER BY `user_users`.`username` ASC
The result is empty. If you remove the WHERE condition acl_usersroles
.acl_roles_id
= 7, the result is again all users with the role 8. But trying to force more than one role at the same time gives an empty result.
Does anyone have an idea how to solve this?