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 | [email protected] | 165 | 10100101 | 2 | [email protected] | 13 | 00001101 | 3 | [email protected] | 33 | 00100001 | 4 | [email protected] | 8 | 00001000 | 5 | [email protected] | 5 | 00000101 +---------+--------------------+-----------------+
Now I want to SELECT users by specific privileges bitmask (by user_privileges
column).
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? Also, is it possible if field user_privileges is text
, containing hexadecimal numbers, instead of integers? I need working mysql query example.
Note: This is just a simple example with 8-bit privilege-set. 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.
The GRANT statement grants privileges to MySQL user accounts. GRANT also serves to specify other account characteristics such as use of secure connections and limits on access to server resources.
[...] 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.
So you can try:
Select * from users WHERE (user_privileges & 1) >0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With