Is it possible to perform a bitwise group function?

后端 未结 5 1003
轻奢々
轻奢々 2020-12-09 10:02

I have a field in a table which contains bitwise flags. Let\'s say for the sake of example there are three flags: 4 => read, 2 => write, 1 => execute a

5条回答
  •  旧时难觅i
    2020-12-09 10:26

    You would need to know the possible permission components (1, 2 and 4) apriori (thus harder to maintain), but this is pretty simple and would work:

    SELECT user_id,
           MAX(BITAND(permissions, 1)) +
           MAX(BITAND(permissions, 2)) +
           MAX(BITAND(permissions, 4)) all_perms
    FROM permissions
    GROUP BY user_id
    

提交回复
热议问题