问题描述
root @ dev>创建表foo(name varchar(255));
查询OK,0行受影响(0.02秒)
root @ dev>插入foo值('with\\\slash');
查询确定,1行受影响(0.00秒)
root @ dev>插入到foo值('\\slash');
查询确定,1行受影响(0.00秒)
root @ dev>从foo中选择*,其名称如%\\\\%;
空集(0.01秒)
root @ dev>从foo中选择*
+ ------------ +
|名称|
+ ------------ +
| with\slash |
| \slash |
+ ------------ +
设置中的2行(0.00秒)
root @ dev>从foo中选择*,其名称如%\\\\%;
空设置(0.00秒)
root @ dev>选择*从foo,其中名称像二进制'%\\\\%';
+ ------------ +
|名称|
+ ------------ +
| with\slash |
| \slash |
+ ------------ +
2行(0.00秒)
根据MySQL文档:
%\\\\ \\\%
是正确的操作数,但为什么它不产生结果?
编辑:
我正在测试的数据库在将character_set_database设置为utf8。为了进一步的调查,我在一个数据库中创建了一个相同的设置,该数据库的character_set_database设置为latin1,并且猜测什么,'%\\\\\%'
!
编辑:
问题可以复制,这是字段排序规则的问题。详细信息:
在MySQL 5.6.10中,使用文本字段排序规则utf8mb4_unicode_520_ci可以通过使用5个反斜杠字符而不是4,即:
select * from foo其中名称像二进制'%\\\\\%' ;
不知何故,不顾一切的期望,这样可以正确地查找带有反斜杠的所有行。
至少这应该工作,直到上面的MySQL字段对齐错误是固定的。考虑到自发现bug以来已经有5年多了,任何设计这个应用程序的应用程序可能比MySQL更加有用,因此应该是一个非常可靠的解决方法。
It's frustrated with MySQL's pattern escaping used in LIKE operator.
root@dev> create table foo(name varchar(255));
Query OK, 0 rows affected (0.02 sec)
root@dev> insert into foo values('with\\slash');
Query OK, 1 row affected (0.00 sec)
root@dev> insert into foo values('\\slash');
Query OK, 1 row affected (0.00 sec)
root@dev> select * from foo where name like '%\\\\%';
Empty set (0.01 sec)
root@dev> select * from foo;
+------------+
| name |
+------------+
| with\slash |
| \slash |
+------------+
2 rows in set (0.00 sec)
root@dev> select * from foo where name like '%\\\\%';
Empty set (0.00 sec)
root@dev> select * from foo where name like binary '%\\\\%';
+------------+
| name |
+------------+
| with\slash |
| \slash |
+------------+
2 rows in set (0.00 sec)
According to MySQL docs: http://dev.mysql.com/doc/refman/5.5/en/string-comparison-functions.html#operator_like%\\\\%
is the right operand, but why it yields no result?
EDIT:The database I'm testing that in has character_set_database set to utf8. To further my investigation, I created the same setup in a database that has character_set_database set to latin1, and guess what, '%\\\\%'
works!
EDIT:The problem can be reproduced and it's the field collation problem. Details: http://bugs.mysql.com/bug.php?id=63829
In MySQL 5.6.10, with the text field collation utf8mb4_unicode_520_ci this can be achieved by using 5 backslash characters instead of 4, i.e:
select * from foo where name like binary '%\\\\\%';
Somehow, against all expectations, this properly finds all rows with backslashes.At least this should work until the MySQL field collation bug above is fixed. Considering it's been more than 5 years since the bug is discovered, any app designed with this may outlive its usefulness before MySQL is even fixed - so should be a pretty reliable workaround.
这篇关于具有通配符和反斜杠的MySQL LIKE运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!