php - 在 LIKE 查询中查找 % 字符

标签 php sql mysql special-characters

我有一个 SQL 数据库,我想做一个显示所有包含符号“%”的数据的查询。通常,要在数据库中查找字符(例如:“z”),我会使用如下查询:

mysql_query("SELECT * FROM mytable WHERE tag LIKE '%z%'");

但在这里,我想找到 % 字符,但在 SQL 中它是一个 clown ,所以当我写:

mysql_query("SELECT * FROM mytable WHERE tag LIKE '%%%'");

它显示了我所有的数据。那么如何在我的 SQL 数据中找到 % 字符呢?

谢谢

最佳答案

您可以使用反斜杠转义 % 符号:

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

引自MySQL 5.0 Reference Manual :: 8.1.1 String小号:

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.

关于php - 在 LIKE 查询中查找 % 字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2890220/

相关文章:

php - Facebook API 搜索 - 搜索群组?

MySql CASE WHEN THEN 也需要 WHERE?

MySQL 查询在 Navicat 中有效,但在其他地方生成 MySQL 语法错误

MySQL 通知程序高 Cpu 负载

php - 想要一并查询帖子和评论吗?

php - 如何使用explode()将2个单词分成2个不同的字符串

sql - 生成一份报告,显示首次登录 7 天后未再次登录的用户百分比

mysql - Django子查询问题 "Subquery returns more than 1 row"

python - 错误 :not all arguments converted during string formatting

php - ajax 到 php 进行 curl 和返回