在LIKE查询中找到%字符 [英] Find the % character in a LIKE query
问题描述
我有一个SQL数据库,我想查询一个显示所有包含符号%"的数据的查询.通常,要在数据库中找到一个字符(例如:"z"),我会使用如下查询:
I've an SQL database and I would like to do a query who show all the datas containing the sign "%". Normally, to find a character (for example: "z") in a database I use a query like this :
mysql_query("SELECT * FROM mytable WHERE tag LIKE '%z%'");
但是在这里,我想找到%字符,但是在SQL中它是一个小丑,所以当我写:
But here, I want to found the % character, but in SQL it's a joker so when I write:
mysql_query("SELECT * FROM mytable WHERE tag LIKE '%%%'");
它显示了我所有的数据.那么如何在我的SQL数据中找到%字符呢?
It show me all my datas. So how to found the % character in my SQL datas ?
谢谢
推荐答案
您可以使用反斜杠转义%
符号:
You can escape the %
sign with a backslash:
SELECT * FROM mytable WHERE tag LIKE '%\%%'
从 MySQL 5.0参考手册:: 8.1.1字符串引用 s:
\%
和\_
序列用于在模式匹配上下文中搜索%
和_
的文字实例,否则它们将被解释为通配符.
The
\%
and\_
sequences are used to search for literal instances of%
and_
in pattern-matching contexts where they would otherwise be interpreted as wildcard characters.
这篇关于在LIKE查询中找到%字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!