2017-06-15 55 views
3

我有一個表有以下數據混淆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查詢是平等的查詢有什麼不同?

回答

2

LIKE默認情況下,與逃脫使用通配符「\」。

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

參考:

但是你可以通過寫作改變
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;'

,因此也是正確的語句,你可以用它來獲得所需的結果!

2

您需要與運營商like

select * from t where a like '%5929%';