Reputation: 2286
I have 5 tables in MySQL, but i believe this is a SQL problem
USERS
USER_INFO
USER_RIGHTS
101 = read right
102 = write right
GROUPS
POSTS
Now i want to select for user 2 for example, all posts made by any user where user 2 has the read right 101 in that group
For example i have 3 post
post 1 posted in group 1 post 2 posted in group 1 post 3 posted in group 2 post 4 posted in group 1
and
WHERE user2 has 101 right in group 1 ==> SHOW post1 2 3
or
WHERE user2 has 101 right in group 2 ==> SHOW post2
or
WHERE user2 has 101 right in both group 1 and group 2 ==> SHOW post2
What i have so far is
SELECT * FROM posts
INNER
JOIN user_info
ON posts.posted_by = user_info.user_id
INNER
JOIN groups
ON posts.posted_in = groups.group_id
INNER
JOIN user_rights
ON user_rights.user_id = user_info.user_id
WHERE user_rights.right = '101' AND user_info.user_id = ".$UserId." ORDER BY id DESC;
This piece of code returns me all posts 2 times.
Upvotes: 1
Views: 115
Reputation: 2123
Im pretty much convinced this
select * from posts
inner join
(select group from user_rights where right = 101 and user_id = 1) as rights
on posts.posted_in = rights.group
would work in SQL server (dont have one here to check the syntax).
On one side im selected all the groupswhere user with id 1 has a read permssion
On the other i select the posts.
All is left is the inner join between them.
Upvotes: 1