问题描述
以下Postgres表包含一些示例内容,其中二进制数据以位变化(https://www.postgresql.org/docs/10/datatype-bit.html)的形式存储:
ID | Binary data
----------------------
1 | 01110
2 | 0111
3 | 011
4 | 01
5 | 0
6 | 00011
7 | 0001
8 | 000
9 | 00
10 | 0
11 | 110
12 | 11
13 | 1
Q:是否有任何查询(本机sql或Postgres函数)返回二进制数据字段等于目标位数组的 all 子字符串的所有行。为了更加清楚,让我们看一下示例搜索值 01101 :
- 01101->没有结果
- 0110->没有结果
- 011-> 3
- 01-> 4
- 0-> 5,10
返回的结果应包含3、4、5和10行。
编辑: 有效的查询为(感谢Laurenz Albe):
SELECT * FROM table WHERE '01101' LIKE (table.binary_data::text || '%')
此外,我发现有关固定大小与可变位的Postgres钻头的讨论很有帮助: PostgreSQL Bitwise operators with bit varying "cannot AND bit strings of different sizes"
解决方法
怎么样
WHERE '01101' LIKE (col2::text || '%')
,
我认为您正在寻找按位和:
where col2 & B'01101' = col2