在MySQL数据库中搜索时匹配'%'符号 [英] Match '%' sign when searching in MySQL database
问题描述
我想在MySQL中匹配此'wildcard %'
.
我尝试使用转义\%
,但无法正常工作.
I would like to match this 'wildcard %'
in MySQL.
I tried using escape \%
and it is not working.
推荐答案
默认转义字符为\
.因此,只需在%
前面加上\
作为前缀:\%
:
The default escape character is \
. So just prefix %
with a \
as: \%
:
手册 清楚地说:
The manual clearly says:
测试a的文字实例 通配符,其前面是 转义字符.如果你不 指定ESCAPE字符,"\"为
To test for literal instances of a wild-card character, precede it by the escape character. If you do not specify the ESCAPE character, "\" is assumed.
在Stack%Overflow
中搜索%
:
mysql> select 'Stack%Overflow' like '%\%%';
+------------------------------+
| 'Stack%Overflow' like '%\%%' |
+------------------------------+
| 1 | <----- Found
+------------------------------+
1 row in set (0.00 sec)
在StackOverflow
中搜索%
:
mysql> select 'StackOverflow' like '%\%%';
+-----------------------------+
| 'StackOverflow' like '%\%%' |
+-----------------------------+
| 0 | <----- Not Found
+-----------------------------+
1 row in set (0.00 sec)
如果要从PHP调用此查询,则必须使用\\
.这是因为即使PHP使用\
作为转义字符.因此,要使MySQL获得\
,您需要在PHP中具有\\
.
If you are calling this query from PHP, you'll have to use \\
. This is because even PHP uses \
as the escape character. So make MySQL get a \
you need to have \\
in PHP.
这篇关于在MySQL数据库中搜索时匹配'%'符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!