php - 使用 like 语句仅返回出现 x 次的命中

标签 php mysql

我有一个类似 sql 的语句:

Select * FROM table WHERE content LIKE '%word%'

这将返回在列内容中找到“word”的所有行。但是是否可以向该语句添加一个参数,以便我只返回找到“单词”两次(或三次、四次或五次……)次的行。

最佳答案

窃取@thebjorn 的评论但使其更通用:

$theword = "word"; // apply appropriate escaping, especially with user input
                   // you will also need to manually escape % and _
$count = 4;
$like_condition = str_repeat("%".$theword,$count)."%";

$query = "select * from table where content like '".$like_condition."'";

关于php - 使用 like 语句仅返回出现 x 次的命中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21970971/

相关文章:

php - 使用 PHP 从格式化日期获取格式字符串

php - MySQL查询结果按查询顺序

javascript - 如何将变量从 PHP 传递到 Javascript

php - 从具有 2 种选择的表中选择 n 行

mysql 正则表达式接受所有国际重音符号,不接受逗号或感叹号

mysql - AWS - 用于访问 RDS 的 Bastion ssh 隧道 - 连接到 SSH 服务器但不连接 MySQL 服务器

php - 如何使用 Limit 在 mysql 查询中始终获取特定行

php mysql,如何获得总时间?

mysql - WordPress - 在 phpmyadmin 中为 WordPress 手动更新 mysql 表后,实时帖子​​反射(reflect)了更改,但帖子编辑器没有反射(reflect)

php - 更新加入 case mysql