首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

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

2012-05-20 
多关键字 多字段经行搜索 并按关键字匹配数量排序我有一个数据库表其中假设有ID title author content等字

多关键字 多字段经行搜索 并按关键字匹配数量排序
我有一个数据库表其中假设有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 

热点排行