假设我爆炸了在搜索中传递的字符串。示例: “如果有只狗” “如果有只狗” (傻傻的美国人)。
我们基于“”爆炸,因此结果…
if there were a dog
现在我要运行一个 SQL select * from table_name query where column_name like '%something%' or column_name like '%somethingelse%'...
SQL select * from table_name query where column_name like '%something%' or column_name like '%somethingelse%'...
我试图确定如何搜索包含最多匹配项的行和表。(即,如果第 45 行包含上述拆分项目中的 4个 ,而第 21 行仅包含 2个 ,则第 45 行应显示在结果的顶部)。
这将是原始的“搜索相关性”逻辑。在SQL中是否有针对此类检索的特定术语?
有什么建议吗?
只需将比较放在order by子句中,使用case语句将它们转换为0/1,然后将它们加起来即可:
order by
case
select * from table_name query where column_name like '%something%' or column_name like '%somethingelse%' order by ((case when column_name like '%something%' then 1 else 0 end) + (case when column_name like '%somethingelse%' then 1 else 0 end) . . . ) desc
我倾向于将查询写为:
select (Match1+Match2+. . .) as NumMatches, <rest of columns> from (select t.*, (case when column_name like '%something%' then 1 else 0 end) as Match1, . . . from tablename ) t order by NumMatches desc