【问题标题】:postgres: Setting the value of an array with a subquery?postgres:使用子查询设置数组的值?
【发布时间】:2011-02-02 20:25:06
【问题描述】:

在 postgres 中,您可以将 INSERT 中的数组值设置为子查询的结果吗?喜欢:

INSERT INTO mytable
VALUES( SELECT list_of_integers FROM someothertable WHERE somekey = somevalue);

mytable 的一列只有 integer[] 类型,而另一列 list_of_integers 也是 integer[] 类型?

【问题讨论】:

    标签: postgresql


    【解决方案1】:

    您需要unnest 函数。我想你会像这样使用它:

    INSERT INTO mytable
    SELECT set_of_integers
    FROM unnest(
      SELECT list_of_integers
      FROM someothertable
      WHERE somekey = somevalue
    ) t(set_of_integers)
    

    但我没有 PostgreSQL 可以亲自试用。

    【讨论】:

      【解决方案2】:

      是的:

      INSERT INTO
           mytable
          (column1, column2, an_array_of_integers_column)
      VALUES
          (2, 'bbb', (SELECT list_of_integers FROM someothertable WHERE somekey = somevalue));
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-05
        • 2015-11-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多