sql - 使用sql批量插入特定值

标签 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 中这样做

最佳答案

手动插入-

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/

相关文章:

MySQL IF/Else 语句结果作为列

postgresql - 无法在 PostgreSQL 中创建类型为 "TIMESTAMP WITHOUT TIME ZONE"的列

postgresql - psql.exe - Windows 中的密码验证失败

ruby-on-rails - Rails 4,在 Postgresql 中保存数组

python - 字符串列表列表到字典列表

python - 从路径名制作树结构

sql - 从 UML 模型生成 SQL?

sql - 在 Microsoft 流分析查询中选择 JSON 数组中的第一个元素

当我为子查询添加别名时,MySQL如何重用选择列别名?

python - 如何将带有索引列表的字典映射到新变量