【问题标题】:Can I index into an array literal in a SELECT statement?我可以在 SELECT 语句中索引数组文字吗?
【发布时间】:2017-06-28 01:08:49
【问题描述】:

我正在从一个 PostgreSQL 数据库(我无法控制)中读取数据,该数据库包含一个类似于枚举的整数列,但枚举值不在数据库中。

这不是我的实际数据,但考虑一个示例students 表:

id | name  | class
==================
1  | Adam  | 1
2  | Bruce | 1
3  | Chris | 3
4  | Dave  | 4

SELECT从此表中,将class列转换为更人性化的东西是很常见的:

SELECT
    id,
    name,
    CASE class
        WHEN 1 THEN 'Freshman'
        WHEN 2 THEN 'Sophomore'
        WHEN 3 THEN 'Junior'
        WHEN 4 THEN 'Senior'
        ELSE 'Unknown'
    END
FROM students

有没有更好的方法来写这个?我尝试构建一个文字数组并使用该列对其进行索引,但如果可能的话,我还没有弄清楚语法。

这些不起作用:

SELECT {"fr", "so", "ju", "se"}[class] FROM students

SELECT '{"fr", "so", "ju", "se"}'[class] FROM students

【问题讨论】:

  • SELECT ('{fr,so,ju,se}'::text[])[class] FROM students 如果你想使用字符串常量而不是数组构造函数。

标签: arrays postgresql select


【解决方案1】:

您可以使用ARRAY keyword 来构造数组:

SELECT (ARRAY['fr', 'so', 'ju', 'se'])[class] FROM students

在 PostgreSQL 中,数组下标以 1 开头,而不是 0,因此如果类似枚举的列使用 0 作为其值之一,您可能需要对其进行移位以获得所需的值。例如,如果class 的值为 0、1、2 和 3,您可以将 1 加到 class

SELECT (ARRAY['fr', 'so', 'ju', 'se'])[class + 1] FROM students

【讨论】:

    【解决方案2】:

    将其设为 CTE 并进行通常的连接:

    with class (class, class_name) as ( values
        (1, 'Freshman'),(2,'Sophomore'),(3,'Junior'),(4,'Senior')
    ), student (id, name, class) as (values
        (1, 'Adam',1),(2,'Bruce',1),(3,'Chris',3),(4,'Dave',4)
    )
    select id, name, class_name
    from
        student
        inner join
        class using (class)
    ;
     id | name  | class_name                                                                                                                                                                                                                       
    ----+-------+------------                                                                                                                                                                                                                      
      1 | Adam  | Freshman                                                                                                                                                                                                                         
      2 | Bruce | Freshman                                                                                                                                                                                                                         
      3 | Chris | Junior                                                                                                                                                                                                                           
      4 | Dave  | Senior       
    

    https://www.postgresql.org/docs/current/static/sql-select.html#SQL-WITH

    WITH 子句允许您指定一个或多个子查询,这些子查询可以在主查询中按名称引用。在主查询期间,子查询有效地充当临时表或视图。

    【讨论】:

      猜你喜欢
      • 2010-11-05
      • 1970-01-01
      • 1970-01-01
      • 2013-04-13
      • 2023-03-08
      • 2013-07-06
      • 1970-01-01
      • 1970-01-01
      • 2023-01-19
      相关资源
      最近更新 更多