MySQL JOIN returning unrelated rows when combined with LEFT JOIN, WHERE and OR -
i have following table structure. idea users have permissions forum either class or specific user overrides. ('action' in both cases enum values 'read' & 'write')
user (id, class) forum (id, name) forum_permissions (forum_id, class_id, action) forum_user_permissions (forum_id, user_id, action)
with following query, i'm getting results based on rows in forum_permissions don't expect. mean every row on forum_permissions forum_id = 3 returned though class_id not match.
select forum.id forum_id, forum.name forum join forum_permissions on forum_permissions.forum_id = forum.id left join forum_user_permissions on ( forum_user_permissions.forum_id = forum.id , forum_user_permissions.user_id = 3 ) (( forum_permissions.class_id = 1 , forum_permissions.action = 'read' ) or ( forum_user_permissions.action = 'read' ))
e.g. this:
forum_id name 1 chat 2 support 3 secret 3 secret 3 secret 3 secret
but expected this:
forum_id name 1 chat 2 support 3 secret
i have made sql fiddle specific example including data http://sqlfiddle.com/#!2/75c3a/5/0
your left join adding lines. mybe if change where
where forum_user_permissions.user_id not null , ( (forum_permissions.class_id = 1 , forum_permissions.action = 'read') or (forum_user_permissions.action = 'read') )
or
select forum.id forum_id, forum.name forum join forum_permissions on forum_permissions.forum_id = forum.id left join forum_user_permissions on ( forum_user_permissions.forum_id = forum.id ) forum_user_permissions.user_id = 3 , ( (forum_permissions.class_id = 1 , forum_permissions.action = 'read') or (forum_user_permissions.action = 'read') )
but depends on results trying get
Comments
Post a Comment