mysql-如何使用特殊字符%(%)和_(下划线)处理查询搜索 [英] mysql - How to handle query search with special characters %(percent) and _(underscore)
问题描述
我的数据库中有2条记录,分别是 Tom%Jerry
和 Ninja_Kids
.一个具有特殊字符%(percent),另一个具有_(下划线).我在mysql终端中尝试了以下命令.
I have 2 records in my database, Tom%Jerry
and Ninja_Kids
. One has a special character %(percent) and the other has _(underscore). I tried the following commands in mysql terminal.
SELECT * FROM CUSTOMER WHERE NAME LIKE '%%%';
SELECT * FROM CUSTOMER WHERE NAME LIKE '%_%';
这两个命令都返回了所有记录.
Both of the commands returned all the records.
我该如何使它们返回具有%
或_
的记录.
How am I gonna make these return the record that has either %
or _
.
我知道%
在mysql中像保留字一样被使用,但是如果记录中包含上述特殊字符,该怎么办.
I know that %
is used in mysql like a reserved word but what if there are cases that a record contains special characters mentioned above.
顺便说一句,除了2以外,您还知道其他行为不正常的特殊字符吗?
BTW, aside from the 2 do you know other special characters that don't behave normal?
请帮助.预先感谢.
推荐答案
您需要转义%
和_
. 文档.
SELECT * FROM CUSTOMER WHERE NAME LIKE '%\%%';
SELECT * FROM CUSTOMER WHERE NAME LIKE '%\_%';
这篇关于mysql-如何使用特殊字符%(%)和_(下划线)处理查询搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!