简体   繁体   English

mysql 中的位操作

[英]bit operations in mysql

I am trying to create a simple filtering of records with bit operations by using this manual: https://dev.mysql.com/doc/refman/8.0/en/bit-functions.html我正在尝试使用本手册创建一个简单的位操作记录过滤: https://dev.mysql.com/doc/refman/8.0/en/bit-functions.html

I have four properties that are defined and based on certain content:我有四个基于特定内容定义的属性:

  • Filter 1 (field1) gets the value 1 (binary 1) Filter 1(field1)获取值1(二进制1)
  • Filter 2 (field2) gets the value 2 (binary 10) Filter 2(field2)获取值2(二进制10)
  • Filter 3 (field3) gets the value 4 (binary 100)过滤器 3 (field3) 获取值 4(二进制 100)
  • Filter 4 (field4) gets the value 8 (binary 1000)过滤器 4 (field4) 获取值 8(二进制 1000)

I set the values with an update:我通过更新设置值:

UPDATE table SET filter = 1 where field1 = "a";
UPDATE table SET filter = filter|2 where field2 = "b";
UPDATE table SET filter = filter|4 where field3 = "c";
UPDATE table SET filter = filter|8 where field4 = "d";

Now the column is filled for the different properties.现在该列已填充不同的属性。 I now have values between 0 (no property applies) and 15 (all properties apply).我现在的值介于 0(没有属性适用)和 15(所有属性都适用)之间。

How do I manage to use them now?我现在如何设法使用它们? If I want to use eg the filters 1,2 and 4, I get with:如果我想使用例如过滤器 1,2 和 4,我得到:

select * from table where filter = 1|2|8; 

I get the value "11".我得到值“11”。 But actually, "15" should also match, since all four properties are applied here.但实际上,“15”也应该匹配,因为这里应用了所有四个属性。

I had no success with this, too:我也没有成功:

select * from table where filter & (1|2|8); 

Can someone help me?有人能帮我吗? Or am I completely wrong?还是我完全错了?

Try WHERE (filter & (1|2|8)) = (1|2|8) .尝试WHERE (filter & (1|2|8)) = (1|2|8)

But please be aware that this bitmasking approach can't exploit indexes, so it will scale up poorly to megarow tables.但请注意,这种位掩码方法不能利用索引,因此它很难扩展到大行表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM