使用 SQLite 进行位掩码分组
当每个表都有一个用于保存一些位掩码枚举的字段时,我有一个 sqlite 表。
我想执行一项查询,该查询将返回所有结果并将所有枚举分组到一个字段中:
Services: TEXT name,INTEGER service,INTEGER mask
SELECT service,XXX FROM Services GROUP BY service
基本上,我希望 XXX 是所有掩码的位掩码 OR (|) 的结果根据该服务。
数据:
'a1',1,1
'a2',1,2,
'a3',1,4,
'a4',1,8,
'b1',2,1,
'b2',2,3,
'b3',2,2
因此,我想得到以下行:
1,15 (1|2|4|8)
2,3 (1|3|2)
谢谢
编辑: 在我原来的问题中,我忘记提及每个掩码不是单个位,而是可以是多个位的掩码(我更改了第二个示例以反映这一点)。
I have an sqlite table when each table has a field which I use to hold some bitmask enum.
I would like to perform a query which will return me all results and group all enums into one field:
Services: TEXT name,INTEGER service,INTEGER mask
SELECT service,XXX FROM Services GROUP BY service
Basically, I would like XXX to be the result of bitmask OR (|) of all masks per that service.
Data:
'a1',1,1
'a2',1,2,
'a3',1,4,
'a4',1,8,
'b1',2,1,
'b2',2,3,
'b3',2,2
So as a result, I would like to get the following rows:
1,15 (1|2|4|8)
2,3 (1|3|2)
Thanks
EDIT:
In my original question, I've forgot to mention that each mask is not a single bit but can be a mask of multiple bits (I've changed the second example to reflect that).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
SQLite支持自定义聚合函数;根据您的设置,您可以注册自定义函数来轻松完成此操作。使用 C API:
然后在 SQL 中,您应该能够执行以下操作:
如果您使用 PHP,则可以 也从 PHP 定义自定义聚合函数。
SQLite supports custom aggregate functions; depending on your set up you could register a custom function to do this pretty easily. Using the C API:
Then within your SQL, you should be able to do:
If you're using PHP, you can define a custom aggregate function from PHP, too.
SQL 中整数的位掩码 OR (|) 操作很简单,就是将每个键的 2 个值的唯一幂相加:
编辑:当您知道位图域时,您可以将值按位和求和:
您可以扩展max(mask&bit) 逻辑,用于存储在位图掩码中的所有已知 2 的幂。
A bitmask OR (|) operation of integers in SQL is simple matter of summing up unique power of 2 values for each key:
EDIT: When you know the bitmap domain, you can bitand the value into its parts and sum:
You can extend the max(mask&bit) logic for all know powers of 2 you are storing in the bitmap mask.