日期:2014-05-18  浏览次数:20478 次

多关键字 多字段经行搜索 并按关键字匹配数量排序
我有一个数据库表其中假设有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 cnt
from tb  
) t where cnt > 0 order by cnt desc