混淆mysql像查询与转义字符?

时间:2017-06-15 14:41:52

标签: mysql

我有一张表有以下数据

select * from t;
+----+----------------+
| id | a              |
+----+----------------+
|  1 | u5929u732b     |
|  2 | \u5929\u732b   |
|  3 | \\u5929\\u732b |
+----+----------------+

查询结果使用等于

select * from t where a = '\u5929\u732b';
+----+------------+
| id | a          |
+----+------------+
|  1 | u5929u732b |
+----+------------+

select * from t where a = '\\u5929\\u732b';
+----+--------------+
| id | a            |
+----+--------------+
|  2 | \u5929\u732b |
+----+--------------+

select * from t where a = '\\\\u5929\\\\u732b';
+----+----------------+
| id | a              |
+----+----------------+
|  3 | \\u5929\\u732b |
+----+----------------+

这些结果就像我预期的那样没问题,然后我就像查询结果一样,在这个时候我很困惑

# as I expected
select * from t where a like '\u5929\u732b';
+----+------------+
| id | a          |
+----+------------+
|  1 | u5929u732b |
+----+------------+

# I do not understand I think it should return result of id = 2
select * from t where a like '\\u5929\\u732b';
+----+------------+
| id | a          |
+----+------------+
|  1 | u5929u732b |
+----+------------+

# I think it should return result of id = 3
select * from t where a like '\\\\u5929\\\\u732b';
+----+--------------+
| id | a            |
+----+--------------+
|  2 | \u5929\u732b |
+----+--------------+

那么为什么like查询与同等查询不同?

3 个答案:

答案 0 :(得分:2)

\是mysql中的转义字符。这意味着声明

select * from t where a like '\\u5929\\u732b';

相当于

select * from t where a like 'u5929u732b;

所以返回的答案是正确的。

因此,您的第三个查询(select * from t where a like '\\\\u5929\\\\u732b';)是一个要求

的查询

select * from t where a like '\\u5929\\u732b;'

因此也是正确的以及您可以用来获得所需结果的声明!

答案 1 :(得分:2)

您需要在like运算符

中使用通配符
select * from t where a like '%5929%';

答案 2 :(得分:2)

LIKE默认使用'\'进行转义。但你可以写下来改变它:

select * from t where a like '\\u5929\\u732b' ESCAPE '|'

参考: