在LIKE运算符中使用MySQL的模式转义令人沮丧。使用通配符和反斜杠的MySQL LIKE运算符
[email protected]> create table foo(name varchar(255));
Query OK, 0 rows affected (0.02 sec)
[email protected]> insert into foo values('with\\slash');
Query OK, 1 row affected (0.00 sec)
[email protected]> insert into foo values('\\slash');
Query OK, 1 row affected (0.00 sec)
[email protected]> select * from foo where name like '%\\\\%';
Empty set (0.01 sec)
[email protected]> select * from foo;
+------------+
| name |
+------------+
| with\slash |
| \slash |
+------------+
2 rows in set (0.00 sec)
[email protected]> select * from foo where name like '%\\\\%';
Empty set (0.00 sec)
[email protected]> select * from foo where name like binary '%\\\\%';
+------------+
| name |
+------------+
| with\slash |
| \slash |
+------------+
2 rows in set (0.00 sec)
根据MySQL的文档:http://dev.mysql.com/doc/refman/5.5/en/string-comparison-functions.html#operator_like %\\\\%
是正确的操作数,但为什么它的产量没有结果?
编辑: 我测试的数据库中的character_set_database设置为utf8。为了进一步调查,我在character_set_database设置为latin1的数据库中创建了相同的设置,并猜测'%\\\\%'
的工作原理!
编辑: 该问题可以重现,它是现场整理问题。详细信息:http://bugs.mysql.com/bug.php?id=63829
当我完全使用你的命令时,'select * from foo'名称'%\\\\%';'适用于我。尽管我不明白为什么它不适合你,但我很遗憾,我很想知道。 –
它可能与数据库字符集有关。我已更新原始帖子。 – EnToutCas
选中此项: - 'select @@ session.sql_mode; select @@ global.sql_mode;' – ajreal