在MySQL数据库中搜索时匹配'%'符号 [英] Match '%' sign when searching in MySQL database

查看:228
本文介绍了在MySQL数据库中搜索时匹配'%'符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆