我有一个数据库表其中假设有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