我的 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 中这样做
最佳答案
手动插入-
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
关于sql - 使用sql批量插入特定值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19674215/