sql - SQL查询可对结果进行排名

标签 sql sqlite

我正在对具有titlecontenttag列的表实施基本搜索,只需在列值上使用LIKE即可。我希望为所有基于title匹配的行分配3点,基于tag匹配的行获得2点,并基于content进行匹配的行得到1点。

是否有可能将所有这些都放入查询中,而不需要使用3个查询?我目前有:

SELECT * FROM node WHERE title LIKE '%keywords%'


我正在使用SQLite以防万一。

最佳答案

使用case

select (case when <title condition> then 3
             when <content condition> then 2
             when <tag condition> then 1
        end) as points, t.*
from t
where <title condition> or <content condition> or <tag condition>;


我不知道条件是什么。您可以填写。

如果有可能,您希望这些点相加(一行最多总计6个点),则逻辑类似:

select ((case when <title condition> then 3 else 0 end) +
        (case when <content condition> then 2 else 0 end) +
        (case when <tag condition> then 1 else 0 end)
       ) as points, t.*
from t
where <title condition> or <content condition> or <tag condition>;

关于sql - SQL查询可对结果进行排名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41126118/

相关文章:

sql - 如何选择 SQL 数据库中的第一个奇数/偶数主键

java - MVC 实践下何时创建新的类/arrayList 与何时仅依赖数据库

sql - 删除所有表时的 Oracle 性能

针对两个表的 SQL 查询

java - ListView内存问题

android - SQLite异常: No Such Column

java - Android,并创建一个非常简单的数据库

java - 在 Java SQL 中自动关闭结果集

c# - 什么是好的 Entity Framework 替代品

sql - Rownum 子句之间不起作用 - Oracle