【问题标题】:Advanced SQLite ORDER BY query高级 SQLite ORDER BY 查询
【发布时间】:2018-04-02 17:45:42
【问题描述】:

我对以一种非常特殊的方式对光标进行排序很感兴趣。在我的程序中,用户可以输入搜索查询。我想返回按项目排序的结果 starting 以及他们输入的查询,然后是简单 包含 的其余项目em> 查询。

例如:用户输入“de”。我希望我的光标以这样一种方式排序>包含“de”。

这是我希望光标的样子:

Deathless Behemoth
Deathmark
Deathmark Prelate
Deathmask Nezumi
Deathmist Raptor
Deathpact Angel
Acid Web Spider
Careful Consideration
Hell-Bent Raider
Jhovall Rider
Living Death

以“de”开头的光标高于仅包含“de”的所有其他内容。

PS 我在这个网站上遇到了一些人们使用 ORDER BY FIELD() 函数的答案。那种看起来像我想要使用的,但我得到一个错误

Error while executing SQL query on database 'database_name': no such function: FIELD

【问题讨论】:

    标签: sql sqlite sorting cursor


    【解决方案1】:

    您可以将各种表达式放在 ORDER BY 子句中,并使用 CASE 的形式提出“首先放置以 'de' 开头的内容”的表达式:

    order by case when lower(user_type) like 'de%' then 0 else 1 end,
             lower(user_type)
    

    因此,当user_type 列以'de' 开头时,case 表达式的计算结果为0,否则它的计算结果为1,并且由于0 排在1 之前,因此以'de' 将首先出现。

    field 函数是,AFAIK,特定于 MySQL。

    【讨论】:

    • 哇,这就像一个魅力。我还有很多关于 SQLite 查询的知识要学习。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-27
    • 1970-01-01
    • 1970-01-01
    • 2020-09-25
    • 2017-05-23
    相关资源
    最近更新 更多