mysql - sql 按最匹配记录排序
问题描述
我编写了 sql 查询来匹配关键字中的任何单词。现在我想订购最匹配的单词。我怎样才能做到这一点。
这是我的 sql 查询。
select a.itemId, a.tags from items a
where a.quantity <> 0 and a.deleted <> 1 and a.tags rlike replace("master guide grade 10", ' ', '|')
例如,如果有记录“主指南等级 10”,那么它首先出现,然后如果有记录“主指南等级 6” ,那么我想获得结果集。我怎样才能做到这一点。
提前致谢!
解决方案
您可以分别查找每个值并将匹配项相加:
select a.itemId, a.tags
from items a
where a.quantity <> 0 and
a.deleted <> 1 and
a.tags rlike replace('master guide grade 10', ' ', '|')
order by (a.tags like '%master%') + (a.tags like '%guide%') + (a.tags like '%grade%') + (a.tags like '%10%') desc
推荐阅读
- json - 一个简单的 json 请求返回一个奇怪的字符串而不是 json 本身
- javascript - 无法在 1 个路由中链接 2 个请求
- symfony - php 风暴 2018 和 symfony 3.4
- python - Django:忽略查询集中特定查询的空格
- google-cloud-platform - Cloud Spanner 不应该使用二级索引
- c# - 如何将数组的每个部分都通过测试以返回输出?
- powershell - 如何使用带有 forfile 的 if 语句来匹配模式并在超过 x 天时删除?
- shell - 如何在 Unix 中跳转到文件的第 n 个字符
- sql - 如何找到每个病房最高的病人
- python - pybind11:如何使用上下文管理器实现