mysql多词按权重匹配搜索

 
更多

有四个中文词(中国,美国,法国,日本),请写一个sql语句,按排列组合的形式去数据库表查询,假设表名为article,要查询的字段名是 content,按匹配的词语多少依次显示结果,例如同时匹配到4个词的就放在最前面,仅匹配到其中一个词的排在最后面。

SELECT   
    content,  
    (  
        CASE   
            WHEN content LIKE '%中国%' AND content LIKE '%美国%' AND content LIKE '%法国%' AND content LIKE '%日本%' THEN 4  
            WHEN content LIKE '%中国%' AND content LIKE '%美国%' AND content LIKE '%法国%' OR   
                 content LIKE '%中国%' AND content LIKE '%美国%' AND content LIKE '%日本%' OR  
                 content LIKE '%中国%' AND content LIKE '%法国%' AND content LIKE '%日本%' OR  
                 content LIKE '%美国%' AND content LIKE '%法国%' AND content LIKE '%日本%' THEN 3  
            WHEN content LIKE '%中国%' AND content LIKE '%美国%' OR  
                 content LIKE '%中国%' AND content LIKE '%法国%' OR  
                 content LIKE '%中国%' AND content LIKE '%日本%' OR  
                 content LIKE '%美国%' AND content LIKE '%法国%' OR  
                 content LIKE '%美国%' AND content LIKE '%日本%' OR  
                 content LIKE '%法国%' AND content LIKE '%日本%' THEN 2  
            WHEN content LIKE '%中国%' OR content LIKE '%美国%' OR content LIKE '%法国%' OR content LIKE '%日本%' THEN 1  
            ELSE 0  
        END  
    ) AS match_count  
FROM   
    article  
ORDER BY   
    match_count DESC, content;
打赏

本文固定链接: https://www.cxy163.net/archives/2971 | 绝缘体

该日志由 绝缘体.. 于 2024年05月09日 发表在 未分类 分类下, 你可以发表评论,并在保留原文地址及作者的情况下引用到你的网站或博客。
原创文章转载请注明: mysql多词按权重匹配搜索 | 绝缘体
关键字: , , , ,

mysql多词按权重匹配搜索:等您坐沙发呢!

发表评论


快捷键:Ctrl+Enter