【问题标题】:Having trouble with Postgres unnest array syntaxPostgres 未嵌套数组语法有问题
【发布时间】:2020-08-11 18:22:16
【问题描述】:

我正在寻找有关执行此插入的最佳方法的指导。我正在尝试为 role_id 58385 创建 11 个条目,同时遍历每个数组的值。我是 PostgreSQL 新手,需要一些指导来了解我在这种情况下做错了什么。

INSERT INTO public.acls (role_id, acl_id, update, can_grant, retrieve, create, archive) VALUES (
           '58385', 
           unnest(array[1,14,20,21,22,24,25,26,36,300,302]), 
           unnest(array[f,f,t,t,f,f,f,t,f,t,t]), 
           unnest(array[f,f,f,f,f,f,f,f,f,f,f]), 
           unnest(array[t,t,t,t,t,t,t,t,t,t,t]), 
           unnest(array[f,f,t,t,f,f,f,t,f,t,t]), 
           unnest(array[f,f,f,f,f,f,f,f,f,f,f])
           )

每个数组都需要SELECT 子查询吗?或者我可以从六个数组中创建一个数组并插入它们。

【问题讨论】:

  • 我很好奇。为什么使用这种复杂的语法而不是带有常量值的简单 values() 子句?

标签: arrays postgresql insert


【解决方案1】:

单个select 可以为您完成,但tf 需要truefalse

select '58385',
           unnest(array[1,14,20,21,22,24,25,26,36,300,302]),
           unnest(array[false,false,true,true,false,false,false,true,false,true,true]),
           unnest(array[false,false,false,false,false,false,false,false,false,false,false]),
           unnest(array[true,true,true,true,true,true,true,true,true,true,true]),
           unnest(array[false,false,true,true,false,false,false,true,false,true,true]),
           unnest(array[false,false,false,false,false,false,false,false,false,false,false])
;
 ?column? | unnest | unnest | unnest | unnest | unnest | unnest 
----------+--------+--------+--------+--------+--------+--------
 58385    |      1 | f      | f      | t      | f      | f
 58385    |     14 | f      | f      | t      | f      | f
 58385    |     20 | t      | f      | t      | t      | f
 58385    |     21 | t      | f      | t      | t      | f
 58385    |     22 | f      | f      | t      | f      | f
 58385    |     24 | f      | f      | t      | f      | f
 58385    |     25 | f      | f      | t      | f      | f
 58385    |     26 | t      | f      | t      | t      | f
 58385    |     36 | f      | f      | t      | f      | f
 58385    |    300 | t      | f      | t      | t      | f
 58385    |    302 | t      | f      | t      | t      | f
(11 rows)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-24
    相关资源
    最近更新 更多