避免在 SQL 中重复预定义值插入
Avoid repeating predefined value in SQL insert into
我希望插入与一对 ID 相关联的几个值,而无需在查询中对这些 ID 进行硬编码。
更具体地说,我有这个 table foo
:
create table if not exists foo(id int, val text);
我可以通过以下方式插入我的值:
insert into foo
values
(10, 'qwe_1'),
(10, 'qwe_2'),
(10, 'qwe_3'),
(20, 'qwe_2'),
(20, 'asd_3'),
(20, 'asd_4');
但我不想重复那些 10
和 20
。
我刚才问过类似的问题(),但它并没有解决我的问题。
我也无法理解如何使用 中建议的连接或类似方法,因为我想为每个 id 添加的值列表是任意的。
虽然不是严格需要,但我想使用 with
语句首先声明我的 ID:
with c (first_id, second_id) as (values (10, 20))
select * from c;
但我不明白如何将它与 insert into
语句结合起来。我有这个 不工作 查询,但它说明了我正在努力实现的目标:
with c (first_id, second_id) as (values (10, 20))
insert into foo
values
(c.first_id, 'qwe_1'),
(c.first_id, 'qwe_2'),
(c.first_id, 'qwe_3'),
(c.second_id, 'qwe_2'),
(c.second_id, 'asd_3'),
(c.second_id, 'asd_4')
from c;
我的理解是 values (...), ...
声明 returns 一个 table 所以也许我缺少的是将这个 table 与 c
table.
您可以使用横向连接:
insert into foo (id, val)
select v.id, v.val
from (values (10, 20)) c(first_id, second_id) cross join lateral
(values (c.first_id, 'qwe_1'),
(c.first_id, 'qwe_2'),
(c.first_id, 'qwe_3'),
(c.second_id, 'qwe_2'),
(c.second_id, 'asd_3'),
(c.second_id, 'asd_4')
) v(id, val);
如果你能使用块结构,我会使用那条路线。
do $$
DEFINE
v_first_id NUMBER := 10;
v_second_id NUMBER := 20;
BEGIN
... Your Insert Statement ...
END; $$
我希望插入与一对 ID 相关联的几个值,而无需在查询中对这些 ID 进行硬编码。
更具体地说,我有这个 table foo
:
create table if not exists foo(id int, val text);
我可以通过以下方式插入我的值:
insert into foo
values
(10, 'qwe_1'),
(10, 'qwe_2'),
(10, 'qwe_3'),
(20, 'qwe_2'),
(20, 'asd_3'),
(20, 'asd_4');
但我不想重复那些 10
和 20
。
我刚才问过类似的问题(
虽然不是严格需要,但我想使用 with
语句首先声明我的 ID:
with c (first_id, second_id) as (values (10, 20))
select * from c;
但我不明白如何将它与 insert into
语句结合起来。我有这个 不工作 查询,但它说明了我正在努力实现的目标:
with c (first_id, second_id) as (values (10, 20))
insert into foo
values
(c.first_id, 'qwe_1'),
(c.first_id, 'qwe_2'),
(c.first_id, 'qwe_3'),
(c.second_id, 'qwe_2'),
(c.second_id, 'asd_3'),
(c.second_id, 'asd_4')
from c;
我的理解是 values (...), ...
声明 returns 一个 table 所以也许我缺少的是将这个 table 与 c
table.
您可以使用横向连接:
insert into foo (id, val)
select v.id, v.val
from (values (10, 20)) c(first_id, second_id) cross join lateral
(values (c.first_id, 'qwe_1'),
(c.first_id, 'qwe_2'),
(c.first_id, 'qwe_3'),
(c.second_id, 'qwe_2'),
(c.second_id, 'asd_3'),
(c.second_id, 'asd_4')
) v(id, val);
如果你能使用块结构,我会使用那条路线。
do $$
DEFINE
v_first_id NUMBER := 10;
v_second_id NUMBER := 20;
BEGIN
... Your Insert Statement ...
END; $$