MySQL: Possible to apply the OR operator across multiple selected rows? -
i have 3 mysql tables: users
, roles
, positions
.
the users
table pretty self-explanatory. roles
table list of job titles person might hold, such janitor
, president
, manager
, etc. roles
table has long array of boolean permissions, such access_basement
or user_directory_access
. if role has has bit value set false (or "0") role lacks permission.
where gets tricky user might have multiple roles, hence why connected positions
table, pairing of userid
, roleid
fields. if perform query like:
select * users left join positions on users.userid=positions.userid left join roles on roles.roleid=positions.roleid users.userid=123
i might results like:
+---------+-----------+-----------------+-----------------------+ | name | title | basement_access | user_directory_access | +---------+-----------+-----------------+-----------------------+ | bob | janitor | true | false | +---------+-----------+-----------------+-----------------------+ | bob | president | false | true | +---------+-----------+-----------------+-----------------------+
since bob has 2 roles, has different access each, i'd combine results since mysql query , logical or
operation across rows, resulting in table like:
+---------+-----------------+-----------------------+ | name | basement_access | user_directory_access | +---------+-----------------+-----------------------+ | bob | true | true | +---------+-----------------+-----------------------+
so question is: is possible apply or operator across multiple selected mysql rows?
thanks!
one way solve if use values 0 , 1 role permissions. , use query like:
select u.name, sum(r.basement_access) basement_access, sum(r.user_directory_access) user_directory_access users u left join positions p on u.userid=p.userid left join roles r on r.roleid=p.roleid u.userid=123 group u.userid;
Comments
Post a Comment