[英]Mysql: How do I select a if a bit on a specific place is 1?
I have a mysql column with an integer containing info about the by a user selected options. 我有一个mysql列,其中包含一个整数,其中包含有关用户选择的选项的信息。
Now, for example, the number 42 = 2 + 8 + 32 = 2^ 1 + 2^ 3 + 2^ 5 , so a user with options = 42 has option 1,3 and 5 selected. 现在,例如,数字42 = 2 + 8 + 32 = 2 ^ 1 + 2 ^ 3 + 2 ^ 5 ,因此具有options = 42的用户选择了选项1,3和5。
How do I select all the users with (at least) option 5 selected? 如何选择(至少)选项5被选中的所有用户? What would be a correct working sql query? 什么是正确的工作SQL查询?
SELECT * FROM `users` WHERE ???(`options`) = ???(5);
Thanks in advance! 提前致谢!
IF options
is an integer type, you can select the rows with at least bit 5 set in their options like this: 如果options
是整数类型,则可以选择在其选项中设置至少第 5位的行,如下所示:
SELECT * FROM `users` WHERE `options` >= POW(2, 5)
To select only columns with bit 5 set in their options, do 要仅选择在其选项中设置了第5位的列,请执行
SELECT * FROM `users` WHERE (`options` & POW(2, 5))
Try bitwise operations: 尝试按位操作:
http://dev.mysql.com/doc/refman/5.0/en/bit-functions.html http://dev.mysql.com/doc/refman/5.0/en/bit-functions.html
in your code (MySql) the condition could be 在您的代码(MySql)中,条件可能是
SELECT *
FROM `users`
WHERE (`options` & (1 << 5)) <> 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.