假设BINARY
列包含以下值:
1001001
0101001
1110110
1101101
与二元模式匹配的最有效方法是什么? 想到任何非正则表达方法?
*1*1**1
以上示例中的匹配值:
0101001
1101101
答案 0 :(得分:2)
将其视为二进制数,然后使用布尔运算。
mysql> SELECT CONV('0101001', 2, 10) & b'0101001' = b'0101001';
+--------------------------------------------------+
| CONV('0101001', 2, 10) & b'0101001' = b'0101001' |
+--------------------------------------------------+
| 1 | -- means TRUE
+--------------------------------------------------+
1 row in set (0.00 sec)
mysql> SELECT CONV('0000000', 2, 10) & b'0101001' = b'0101001';
+--------------------------------------------------+
| CONV('0000000', 2, 10) & b'0101001' = b'0101001' |
+--------------------------------------------------+
| 0 | -- means FALSE
+--------------------------------------------------+
1 row in set (0.00 sec)
mysql> SELECT CONV('0000111', 2, 10) & b'0101001' = b'0101001';
+--------------------------------------------------+
| CONV('0000111', 2, 10) & b'0101001' = b'0101001' |
+--------------------------------------------------+
| 0 |
+--------------------------------------------------+
答案 1 :(得分:1)
如果长度值固定为
,您可以使用substring()
mysql> select substring('0101001',2,1) as pos1 ,substring('0101001',4,1) pos2,substring('0101001',7,1) pos3;
+------+------+------+
| pos1 | pos2 | pos3 |
+------+------+------+
| 1 | 1 | 1 |
+------+------+------+
1 row in set (0.00 sec)
所以你可以在select语句中使用上面的
select * from table_name
where
substring(col_val,2,1) = 1
and substring(col_val,4,1) = 1
and substring(col_val,7,1) = 1