![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
I have problem joing tables. one table is user table the other one is user role table and one user can have multiple roles I want to find out if there is any users without a specific roles user table 1. Jone Done 2. Micheal Canue user role uid roles_id 1 * * 2 1 * * 3 2 * * 1 2 * * 2 3 * * 1 and i want to find out if there is any user without role 3? |
#3
| |||
| |||
|
|
On Feb 18, 8:54 am, cake-learner <sh.koiz... (AT) gmail (DOT) com> wrote: I have problem joing tables. one table is user table the other one is user role table and one user can have multiple roles I want to find out if there is any users without a specific roles user table 1. Jone Done 2. Micheal Canue user role uid roles_id 1 2 1 3 2 1 2 2 3 1 and i want to find out if there is any user without role 3? SELECT ut.name, FROM user_table ut LEFT JOIN user_role ur ON ut.uid = ur.uid AND ur.doles_id = 3 WHERE ur.id IS NULL |

#4
| |||
| |||
|
|
On 18-02-11 10:04, Captain Paralytic wrote: On Feb 18, 8:54 am, cake-learner <sh.koiz... (AT) gmail (DOT) com> wrote: I have problem joing tables. one table is user table the other one is user role table and one user can have multiple roles I want to find out if there is any users without a specific roles user table 1. Jone Done 2. Micheal Canue user role uid roles_id 1 2 1 3 2 1 2 2 3 1 "I want to find out if there is any users without a specific roles" SELECT ut.name, FROM user_table ut LEFT JOIN user_role ur ON ut.uid = ur.uid AND ur.roles_id IS NULL |
#5
| |||
| |||
|
|
On 18-02-11 10:04, Captain Paralytic wrote: On Feb 18, 8:54 am, cake-learner <sh.koiz... (AT) gmail (DOT) com> wrote: I have problem joing tables. one table is user table the other one is user role table and one user can have multiple roles I want to find out if there is any users without a specific roles user table 1. Jone Done 2. Micheal Canue user role uid roles_id 1 * * 2 1 * * 3 2 * * 1 2 * * 2 3 * * 1 and i want to find out if there is any user without role 3? SELECT * ut.name, FROM user_table ut LEFT JOIN user_role ur ON ut.uid = ur.uid AND ur.doles_id = 3 WHERE ur.id IS NULL "ur.doles_id = 3 WHERE ur.id IS NULL"... Above will not find any records... ![]() SELECT * *ut.name, *FROM user_table ut *LEFT JOIN user_role ur ON ut.uid = ur.uid AND ur.roles_id = 3 And the first question, which was overlooked: "I want to find out if there is any users without a specific roles" SELECT * *ut.name, *FROM user_table ut *LEFT JOIN user_role ur ON ut.uid = ur.uid AND ur.roles_id IS NULL -- Luuk |
#6
| |||
| |||
|
|
I have problem joing tables. one table is user table the other one is user role table and one user can have multiple roles I want to find out if there is any users without a specific roles user table 1. Jone Done 2. Micheal Canue user role uid roles_id 1 2 1 3 2 1 2 2 3 1 and i want to find out if there is any user without role 3? |
|
uid | name | +-----+---------+ 2 | Michael | +-----+---------+ |
#7
| |||
| |||
|
|
On Feb 18, 9:29 am, Luuk <L... (AT) invalid (DOT) lan> wrote: On 18-02-11 10:04, Captain Paralytic wrote: On Feb 18, 8:54 am, cake-learner <sh.koiz... (AT) gmail (DOT) com> wrote: I have problem joing tables. one table is user table the other one is user role table and one user can have multiple roles I want to find out if there is any users without a specific roles user table 1. Jone Done 2. Micheal Canue user role uid roles_id 1 2 1 3 2 1 2 2 3 1 and i want to find out if there is any user without role 3? SELECT ut.name, FROM user_table ut LEFT JOIN user_role ur ON ut.uid = ur.uid AND ur.doles_id = 3 WHERE ur.id IS NULL "ur.doles_id = 3 WHERE ur.id IS NULL"... Above will not find any records... ![]() SELECT ut.name, FROM user_table ut LEFT JOIN user_role ur ON ut.uid = ur.uid AND ur.roles_id = 3 And the first question, which was overlooked: "I want to find out if there is any users without a specific roles" SELECT ut.name, FROM user_table ut LEFT JOIN user_role ur ON ut.uid = ur.uid AND ur.roles_id IS NULL -- Luuk Yes I hit the D instead of the r, but apart from that my query was correct. Your one will not work as you have the NULL test in the wrong place. |
#8
| |||
| |||
|
|
As a sidenote, a question like "What users have all roles" is quite easy to express via nested exist predicates: -- users where it does not exist a role -- that the user don't have select * from user u where not exists ( select 1 from user_role x where not exists ( select 1 from user_role y where x.role_id = y.role_id and y.uid = u.uid ) ); |
#9
| |||
| |||
|
|
Lennart Jonsson: As a sidenote, a question like "What users have all roles" is quite easy to express via nested exist predicates: -- users where it does not exist a role -- that the user don't have select * from user u where not exists ( select 1 from user_role x where not exists ( select 1 from user_role y where x.role_id = y.role_id and y.uid = u.uid ) ); Interesting! I would have counted the total number of roles (R), and then selected the users for which their number of roles is less than R. |
|
uid | name | +-----+---------+ 2 | Michael | +-----+---------+ |
|
uid | name | +-----+---------+ 1 | Jone | 2 | Michael | +-----+---------+ |
![]() |
| Thread Tools | |
| Display Modes | |
| |