如何一次将多个值插入到postgres表中? [英] How do I insert multiple values into a postgres table at once?

查看:76
本文介绍了如何一次将多个值插入到postgres表中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表,我试图一次更新多个值。这是表模式:

I have a table that I am trying to update multiple values at once. Here is the table schema:

    Column     |  Type   | Modifiers 
---------------+---------+-----------
 user_id       | integer | 
 subservice_id | integer |

我有 user_id 并想插入一次有多个 subservice_id Postgres 中是否有语法可以让我做这样的事情

I have the user_id and want to insert multiple subservice_id's at once. Is there a syntax in Postgres that will let me do something like this

insert into user_subservices(user_id, subservice_id) values(1, [1, 2, 3]);

我该怎么做?

推荐答案

尝试:

INSERT INTO user_subservices(user_id, subservice_id) 
SELECT 1 id, x
FROM    unnest(ARRAY[1,2,3,4,5,6,7,8,22,33]) x

演示: http://www.sqlfiddle.com/# !15 / 9a006 / 1

这篇关于如何一次将多个值插入到postgres表中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆