【问题标题】:sql descending alphabetical inside order by case example (firebird)sql按大小写按字母顺序降序排列示例(火鸟)
【发布时间】:2014-02-20 21:54:41
【问题描述】:

这需要在 Firebird 中使用 FlameRobin 完成

我的问题很简单,但我仍然需要帮助

Select * from Clients

order by Case town

when 'amsterdam' Then 1
when 'rotterdam' Then 2
when 'maastricht' Then 3
else 4 end,
Case Gender 
when null then 1
when 'Male' then 2
when 'Female' then 3
else 4 end,

---从这里我想要的就出错了--

Case name
when null then 1
when asc then 2    ( and here I want the names alphabetical descending )
else 3 end 

因为 sql 太有限了,我需要一些帮助

【问题讨论】:

  • dev.mysql.com/doc/refman/5.0/en/case.html 最后一段概述了这种情况。
  • 我不确定你想用你的名字排序做什么。如果有一个名为asc 的名称值您想以某种方式触发降序?
  • 我正在使用 Firebird(我的错误),我的值不被称为(但它们只是名称)我的印象是 asc 用于按字母顺序排列的东西......猜我错了

标签: sql case firebird alphabetical


【解决方案1】:

我认为您最好在您的选择中构建这些计算字段,然后使用它们进行如下排序:

SELECT *,
    (CASE town
        WHEN 'amsterdam' THEN 1
        WHEN 'rotterdam' THEN 2
        WHEN 'maastricht' THEN 3
        ELSE 4
    END CASE) as town_order,
    CASE gender
        WHEN ...
        ...
        ELSE 4
    END CASE) as gender_order,
    CASE gender
        WHEN ...
        ...
        ELSE 3
    END CASE) as name_order
ORDER BY town_order ASC, gender_order ASC, name_order ASC, name DESC

当然,这将是一个非常不理想的查询,不能使用任何类型的索引进行排序。如果你真的想高效查询,你应该在你的表中添加特定的town_ordergender_order等字段并在它们上面放置索引。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-14
    • 1970-01-01
    • 2011-08-14
    • 1970-01-01
    • 1970-01-01
    • 2012-09-20
    • 1970-01-01
    相关资源
    最近更新 更多