Assume I have these two tables:
Table User:
ID Name
1 User A
2 User B
3 User C
4 User D
5 User E
Table Permission:
ID UserID PermissionName
1 1 read
2 3 read
3 3 write
4 5 read
5 5 write
6 5 admin
Now I want to have the following queries:
1- All users with write permission
SELECT u.* FROM user u, permission p WHERE p.userID = u.id AND p.name = 'write' GROUP BY u.id;
Seems to be ok. Returns user 3 and 5.
2- All users without write permission
SELECT u.* FROM user u, permission p WHERE p.userID = u.id AND p.name != 'write' GROUP BY u.id;
Returns: 1,3,5 Expected: 1,2,4
This does not work. It returns every user, except those with only the write permission. So how can I make this work in MySQL and SQLite?
3- Combinations of both
- All users with read and write (3,5)
- All users with read and not write (1)
- etc.