[英]SELECT users from MySQL database by privileges bitmask?
I have users table and want to SELECT some rows by bitmask criteria. 我有用户表,并希望按位掩码标准选择一些行。 I'll try to explain my problem with small example.
我会用小例子来解释我的问题。
Structure of table users 表用户的结构
user_id int [primary key, auto_increment]
user_email varchar(200)
user_privileges int
Note: It has more fields but they are irrelevant for this question. 注意:它有更多字段,但它们与此问题无关。
Filled table may look like this 填充表可能如下所示
+---------+--------------------+-----------------+
| user_id | user_email | user_privileges | << binary
+---------+--------------------+-----------------+
| 1 | john@example.com | 165 | 10100101
| 2 | max@example.com | 13 | 00001101
| 3 | trevor@example.com | 33 | 00100001
| 4 | paul@example.com | 8 | 00001000
| 5 | rashid@example.com | 5 | 00000101
+---------+--------------------+-----------------+
Now I want to SELECT users by specific privileges bitmask (by user_privileges
column). 现在我想通过特定权限bitmask(通过
user_privileges
列)来SELECT用户。
For example: 例如:
My question: Is it possible from query or I have to go all users one-by-one and check this value from PHP code? 我的问题:是否有可能从查询或我必须逐个去所有用户并从PHP代码检查此值? Also, is it possible if field user_privileges is
text
, containing hexadecimal numbers, instead of integers? 另外,如果字段user_privileges是包含十六进制数字而不是整数的
text
,是否可能? I need working mysql query example. 我需要工作的mysql查询示例。
Note: This is just a simple example with 8-bit privilege-set. 注意:这只是一个使用8位特权集的简单示例。 In real environment it may have larger sets (greater integers, more bytes).
在真实环境中,它可能具有更大的集合(更大的整数,更多的字节)。 Creating separate column for each privilege state works fine, but that's not possible solution.
为每个权限状态创建单独的列工作正常,但这不是可能的解决方案。 I'd rather work with hex values, but integers are fine too, something is better than nothing.
我宁愿使用十六进制值,但整数也很好,有些东西比什么都好。
Thanx in advance. Thanx提前。
SELECT
*
FROM
users
WHERE
(user_privileges & <level>) = <level>
<level>
being the access level you want to search on (eg 1, 5, 9, 130, etc.) <level>
是您要搜索的访问级别(例如1,5,9,130等)
[...] want to SELECT some fields
[...]想要选择一些字段
Wrong. 错误。 You want to select some Rows .
您想要选择一些行 。 Columns are usually called fields.
列通常称为字段。
You are supposed to read the Documentation: Bit Functions are documented for mysql. 您应该阅读文档:为mysql记录位功能 。
So you can try: 所以你可以尝试:
Select * from users WHERE (user_privileges & 1) >0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.