本文介绍了是否可以执行按位组功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我在包含按位标志的表中有一个字段。比方说,为了举例,有三个标志: 4 =>阅读,2 =>写,1 =>执行,表看起来像这样 * : user_id |文件|权限 ----------- + -------- + --------------- 1 | a.txt | 6( 1 | b.txt | 4( 2 | a.txt | 4 2 | c.exe | 1( 我有兴趣找到所有拥有在任何记录上设置特定标志(例如:写入)。为了在一个查询中做到这一点,我认为如果你将所有用户的权限组合在一起,你会得到一个单一的值,这是他们权限的总和: user_id | all_perms ----------- + ------------- 1 | 6( 2 | 5( * 我的实际表格不是用来处理文件或文件权限,而是一个例子 有一种方法可以在一个声明中执行此操作吗?我看到它的方式,它与使用GROUP BY的普通聚合函数非常相似: SELECT user_id,SUM(permissions)as all_perms FROM权限 GROUP BY user_id ...但显然,有些神奇的按位或功能,而不是SUM。任何人都知道这样的事情? (对于奖励点,它在oracle中是否有效?) 解决方案 MySQL: SELECT user_id,BIT_OR(permissions)as all_perms FROM权限 GROUP BY user_id 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 and the table looks like this*: user_id | file | permissions-----------+--------+--------------- 1 | a.txt | 6 ( <-- 6 = 4 + 2 = read + write) 1 | b.txt | 4 ( <-- 4 = 4 = read) 2 | a.txt | 4 2 | c.exe | 1 ( <-- 1 = execute)I'm interested to find all users who have a particular flag set (eg: write) on ANY record. To do this in one query, I figured that if you OR'd all the user's permissions together you'd get a single value which is the "sum total" of their permissions: user_id | all_perms-----------+------------- 1 | 6 (<-- 6 | 4 = 6) 2 | 5 (<-- 4 | 1 = 5)*My actual table isn't to do with files or file permissions, 'tis but an exampleIs there a way I could perform this in one statement? The way I see it, it's very similar to a normal aggregate function with GROUP BY:SELECT user_id, SUM(permissions) as all_permsFROM permissionsGROUP BY user_id...but obviously, some magical "bitwise-or" function instead of SUM. Anyone know of anything like that?(And for bonus points, does it work in oracle?) 解决方案 MySQL:SELECT user_id, BIT_OR(permissions) as all_permsFROM permissionsGROUP BY user_id 这篇关于是否可以执行按位组功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-14 09:10