2013-10-30 55 views
1

我在我的Postgres数据库中的以下表的具体值:在表1批量插入使用SQL

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

的示例数据是:

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

现在我想批量插入所有出现的值在col1中将num1转换为字典(它是一个现有的表并已包含值)放入其id列中。以便我的字典包含以下值:

3 str3 
6 str6 
7 str 

此处字典的value列包含“str”+ col2形式的值。

是否有可能这样做的Postgres的

回答

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 
+0

感谢您的答复..我想从col1中从num1 a中选择不同的值nd将它们插入字典 –

+0

简单!只需在选择查询中添加不同的...更新了答案。 – pyrospade

0

INSERT INTO词典(ID ,值)值(1,'aaa'),(2,'bbb'),(3,'ccc')