I would like to run a simple query to get all users of a particular role, the problem is user roles are开发者_JS百科 stored in bitwise numbers using power of 2 pattern.
For example the roles table is.
1 role one
2 role two...
4
8
16
In a user table we have
username other columns roles
billy .... 192949
I'm not sure how to query for a role like that...
For a single role:
DECLARE @RoleOne = 1
SELECT * FROM Users
WHERE (roles & @RoleOne) > 0
Or a member of all multiple roles
DECLARE @MultipleRoles = 1 + 4 + 64
SELECT * FROM Users
WHERE (roles & @MultipleRoles) = @MultipleRoles
(I'm assuming SQL Server but will be very similar for other flavours)
精彩评论