排除不适合掩码的值 - mySQL 中的按位运算
Excluding values that don't fit the mask - Bitwise operations in mySQL
我需要帮助解决问题。我有一个具有位掩码值的用户,比方说 10010001。
列名是 value_1。在本专栏中,我有以下值可以与之比较:
|--|----------|
|id| value_1|
|--|----------|
| 1| 00000001 |
| 2| 10010001 |
| 3| 00000010 |
| 4| 10000001 |
| 5| 10011001 |
|--|----------|
我的查询是这样的:
select value_1 from testdb
where b'10010001' & value_1;
我的结果是 ID 1,2,4,5
这看起来不错 - 但我怎么能排除 id 5 因为最低的第 4 位(从右边数)不在源代码中?
假设您的 value_1 数据类型是字符串
尝试使用适当的转换
select value_1 from testdb
where cast( '10010001' as unsigned ) & cast( value_1 as unsigned ) ;
确保 value_1 的值和掩码的组合位不会产生超过掩码的数字。
where (b'10010001' & value_1) and (cast((b'10010001' | value_1) as unsigned) <= cast(b'10010001' as unsigned))
超出意味着 value_1 的值中至少有一位存在,但掩码(来源)中没有。
测试一下here
我需要帮助解决问题。我有一个具有位掩码值的用户,比方说 10010001。
列名是 value_1。在本专栏中,我有以下值可以与之比较:
|--|----------|
|id| value_1|
|--|----------|
| 1| 00000001 |
| 2| 10010001 |
| 3| 00000010 |
| 4| 10000001 |
| 5| 10011001 |
|--|----------|
我的查询是这样的:
select value_1 from testdb
where b'10010001' & value_1;
我的结果是 ID 1,2,4,5
这看起来不错 - 但我怎么能排除 id 5 因为最低的第 4 位(从右边数)不在源代码中?
假设您的 value_1 数据类型是字符串 尝试使用适当的转换
select value_1 from testdb
where cast( '10010001' as unsigned ) & cast( value_1 as unsigned ) ;
确保 value_1 的值和掩码的组合位不会产生超过掩码的数字。
where (b'10010001' & value_1) and (cast((b'10010001' | value_1) as unsigned) <= cast(b'10010001' as unsigned))
超出意味着 value_1 的值中至少有一位存在,但掩码(来源)中没有。
测试一下here