简体   繁体   English

使用 WITH PostgreSQL 从相等数组插入值

[英]insert values from equal arrays using WITH PostgreSQL

I would like smth like:我想像:

INSERT INTO Transit (idProc, uuidSeg, number) 
select * from 
unnest(array[2, 2]), 
unnest(array ['3ec172b9-b99f-43e2-83bb-527e9b0fb308', '6a72c69c-1083-4c63-83ec-22b0ab512789']::uuid[]), 
unnest(array[1, 2]) where 'some_value' IS NOT NULL;

But this query inserts too many rows:但是这个查询插入了太多的行:

在此处输入图片说明

Inserts correct when I run运行时插入正确

WITH data (idProc, uuidSeg, number)  as
    (VALUES
      (2, '3ec172b9-b99f-43e2-83bb-527e9b0fb308'::uuid, 1),
      (2, '6a72c69c-1083-4c63-83ec-22b0ab512789'::uuid, 2))
INSERT INTO Transit (idProc, uuidSeg, number)
SELECT d.idProc, d.uuidSeg, d.number
FROM data d
WHERE 'some_value' is not null;

在此处输入图片说明

My question is, What way I can rewrite my first query for it would be possible to insert correctly from arrays simultaneously (they are always equal )?我的问题是,我可以通过什么方式重写我的第一个查询,以便可以同时从数组中正确插入(它们总是相等)? In fact, first column (idProc) is not an array, it is a value, that must be inserted so many times as the length of array.事实上,第一列(idProc)不是一个数组,它是一个值,必须插入数组长度的次数。 Is it possible?是否可以?

unnest() can take multiple arguments, unnesting them in parallel: unnest()可以接受多个参数,并行解除它们的嵌套:

select *  
from unnest(array[2, 2], 
            array ['3ec172b9-b99f-43e2-83bb-527e9b0fb308', '6a72c69c-1083-4c63-83ec-22b0ab512789']::uuid[], 
            array[1, 2]
           ) u(idProc, uuidSeg, number)
where 'some_value' IS NOT NULL;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM