Find the % character in a LIKE query

寵の児 提交于 2019-12-11 01:57:03

问题


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%'");

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 '%%%'");

It show me all my datas. So how to found the % character in my SQL datas ?

Thanks


回答1:


You can escape the % sign with a backslash:

SELECT * FROM mytable WHERE tag LIKE '%\%%'

Quoting from MySQL 5.0 Reference Manual :: 8.1.1 Strings:

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.




回答2:


Here is a standard way:

SELECT * FROM mytable WHERE tag like '%|%%' ESCAPE '|'


来源:https://stackoverflow.com/questions/2890220/find-the-character-in-a-like-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!