当前位置: 代码迷 >> Sql Server >> 多关键字 多字段经行搜索 并按关键字匹配数量排序,该如何解决
  详细解决方案

多关键字 多字段经行搜索 并按关键字匹配数量排序,该如何解决

热度:44   发布时间:2016-04-27 12:49:34.0
多关键字 多字段经行搜索 并按关键字匹配数量排序
我有一个数据库表其中假设有ID title author content等字段
现在用户输入一组关键字 数量不等
要求对数据库表中的ID字段外经行模糊匹配搜索
最后将结果按照匹配关键字数量之和排序
假如 title匹配了2个关键字 author匹配了1个关键字 content匹配了4个关键字 即这条数据匹配了7个关键字
我就按7经行排序
请问要怎么实现?


------解决方案--------------------
SQL code
select * from(select *,case when title like '关键字1' then 1 else 0 end + case when title like '关键字2' then 1 else 0 end +...case when title like '关键字n' then 1 else 0 end +case when author like '关键字1' then 1 else 0 end + case when author like '关键字2' then 1 else 0 end +...case when author like '关键字n' then 1 else 0 end  as cntfrom tb  ) t where cnt > 0 order by cnt desc
  相关解决方案