繁体   English   中英

排除不适合掩码的值 - 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 的 value 和 mask 的组合位不会导致超过作为数字的 mask 的数字。

where (b'10010001' & value_1) and (cast((b'10010001' | value_1) as unsigned) <= cast(b'10010001' as unsigned))

超出意味着 value_1 的值中至少存在一位,但掩码(源)中不存在。

在这里测试

假设您的 value_1 数据类型是字符串尝试使用正确的转换

    select value_1 from testdb
    where cast( '10010001' as unsigned )  & cast( value_1 as unsigned ) ;

暂无
暂无

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

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