在mysql where子句中使用“LIKE”时,“%”和“%%”有什么区别?
select * from `wp_users` u where u.user_nicename like "%lastuser%"VS
select * from `wp_users` u where u.user_nicename like "%%lastuser%%"发布于 2011-12-23 21:54:54
当涉及到mysql中的模式匹配时,%%和%没有区别。
我见过开发人员在尝试匹配文字%并为此编写%%时会对此感到困惑。这通常是因为format-strings经常使用双%来表示您希望将其作为精确的文字处理。
LIKE的MySQL文档
字符串的起源是什么,它的去向是什么?
如果将字符串传递给sprintf之类的函数,就会出现我前面提到的格式字符串规则,尽管在这种情况下不会产生混淆。
开发人员希望它是传递给mysql的字符串中的单个%,因此编写了%%。
$query = sprintf (
"SELECT ... FROM ... WHERE id <> %d AND data LIKE '%%hello world%%'",
50
);
// $query => "SELECT ... FROM ... WHERE id <> 50 AND data LIKE '%hello world%'";几个使用LIKE运算符的样本选择
mysql> SELECT 'abc' LIKE 'ab%';
+------------------+
| 'abc' LIKE 'ab%' |
+------------------+
| 1 |
+------------------+
1 row in set (0.01 sec)
mysql> SELECT 'abc' LIKE 'ab%%';
+-------------------+
| 'abc' LIKE 'ab%%' |
+-------------------+
| 1 |
+-------------------+
1 row in set (0.00 sec)
mysql> SELECT 'abc' LIKE 'ab\%';
+-------------------+
| 'abc' LIKE 'ab\%' |
+-------------------+
| 0 |
+-------------------+
1 row in set (0.00 sec)
mysql> SELECT 'ab%' LIKE 'ab\%';
+-------------------+
| 'ab%' LIKE 'ab\%' |
+-------------------+
| 1 |
+-------------------+
1 row in set (0.00 sec)https://stackoverflow.com/questions/8616829
复制相似问题