使用sql批量插入特定值

时间:2013-10-30 04:59:27

标签: sql postgresql dictionary

我的postgres数据库中有以下表格:

create table num1(col1 bigint, col2 bigint, col3 bigint, col4 bigint);
create table dictionary (id bigint, value varchar);

table1中的示例数据是:

5 3 4 2 
9 6 8 2
1 7 8 3

现在我想将num1中col2中存在的所有值批量插入到字典中(它是一个现有的表,并且已经包含值)到其id列中。这样我的字典就包含以下值:

3  str3
6  str6
7  str

这里字典的值列包含“str”+ col2形式的值。

是否可以在postgres中这样做

2 个答案:

答案 0 :(得分:1)

手动执行插入 -

insert into dictionary (id, value)
values (3, 'str3'), (6, 'str6'), (7, 'str7');

从查询结果中插入 -

insert into dictionary (id, value)
select distinct col2, 'str' || col2::text
from num1

答案 1 :(得分:0)

INSERT INTO字典(id,value)值(1,'aaa'),(2,'bbb'),(3,'ccc')