mysql - 按出现次数最多的搜索查询排序
问题描述
我想通过搜索查询在字段标题和文本中的表格中搜索,并且匹配最多的行获胜。我的表格页面如下所示:
------------------------------------------
| id | title | text | status |
------------------------------------------
| 1 | test test | test xx | 200 |
| 2 | nothing | nothing | 200 |
| 3 | test test | test test | 200 |
| 4 | abcefgh | ijkl test | 200 |
| 5 | test | test abc | 200 |
------------------------------------------
预期结果将如下所示(通过搜索查询测试和限制 3选择id、title、text ):
---------------------------------
| id | title | text |
---------------------------------
| 3 | test test | test test |
| 1 | test test | test xx |
| 5 | test | test abc |
---------------------------------
我非常感谢您的帮助,因为我现在已经很长时间找不到解决方案了。提前致谢!
解决方案
你可以做:
select
id, title, text
from t
order by
round(length(title) - length(replace(title, "test", "")) / length("test") +
round(length(text) - length(replace(text, "test", "")) / length("test")
desc
limit 3
推荐阅读
- c++ - 模板函数调用参数构造函数
- angular - ngx-bootstrap 嵌套下拉项绑定
- node.js - 如何使用 grpc 的无聊功能来处理我的 nodejs 程序
- c# - 在.Net 5.0 中配置服务时如何使用配置数据?
- python - 将变量加载到数据框中
- php - 当我尝试使用 phpMailer 从 php 发送电子邮件时,如何解决 SMTP 连接错误?POP/IMAP
- angular - 生成组件未在应用程序模块中以角度创建引用
- cudf - 使用 cudf 读取一个巨大的 csv 文件
- python - 装饰多个 Python 函数
- typescript - 选择嵌套类型 TypeScript