我有一张表有以下数据
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
查询与同等查询不同?
答案 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 '|'
参考: