【问题标题】:Ordering by Postgres hstore attribute not working as expected按 Postgres hstore 属性排序未按预期工作
【发布时间】:2018-07-29 12:09:07
【问题描述】:

我使用 Postgres 10.4,但在一个简单的查询中出现了奇怪的排序行为。

我的表是这样定义的:

CREATE TABLE country (
    id           varchar(2)   NOT NULL PRIMARY KEY,
    order_by     smallint,
    app_locale   boolean      DEFAULT false NOT NULL,
    country      boolean      DEFAULT false NOT NULL,
    lang         boolean      DEFAULT false NOT NULL,
    nationality  boolean      DEFAULT false NOT NULL,
    c_name       hstore,
    l_name       hstore,
    n_name       hstore,
    active       boolean      DEFAULT false NOT NULL,
    language     varchar(6)
);

这个简单的查询应该返回按英文国籍名称排序的国家:

select id, c_name
from country c
where active = true
and   c.nationality = true
and   id in ('de','fr','be','lu','kz','af','ad') -- limit the result a bit
order by c.n_name -> 'en'

意外的结果是:

id  c_name
be  "de"=>"Belgien", "lb"=>"Belsch", "en"=>"Belgium", "fr"=>"Belgique"
fr  "de"=>"Frankreich", "lb"=>"Frankräich", "en"=>"France", "fr"=>"France"
de  "de"=>"Deutschland", "lb"=>"Däitschland", "en"=>"Germany", "fr"=>"Allemagne"
lu  "de"=>"Luxemburg", "lb"=>"Lëtzebuerg", "en"=>"Luxembourg", "fr"=>"Luxembourg"
ad  "en"=>"Andorra"
kz  "en"=>"Kazakhstan"
af  "de"=>"Afghanistan", "en"=>"Afghanistan", "fr"=>"Afghanistan"

这里有什么问题?

【问题讨论】:

    标签: sql postgresql hstore


    【解决方案1】:

    您在order by 中使用了错误的列:

    select id, c_name
    from country c
    where active = true
    and c.nationality = true
    and id in ('de','fr','be','lu','kz','af','ad')
    order by c_name -> 'en'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-04-26
      • 2021-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多