gpt4 book ai didi

postgresql - Postgres : insert value from another table as part of multi-row insert?

转载 作者:行者123 更新时间:2023-11-29 13:39:44 25 4
gpt4 key购买 nike

我在 Postgres 9.6 中工作,想在单个查询中插入多行,使用 INSERT INTO 查询。

我还想从另一个表中选择一个值作为插入的值之一。

这是我试过的:

insert into store_properties (property, store_id) 
values
('ice cream', select id from store where postcode='SW1A 1AA'),
('petrol', select id from store where postcode='EC1N 2RN')
;

但是我在第一个 select 处遇到语法错误。我做错了什么?

请注意,该值是按行确定的,即我不是直接从另一个表中复制值。

最佳答案

demo:db<>fiddle

insert into store_properties (property, store_id) 
values
('ice cream', (select id from store where postcode='SW1A 1AA')),
('petrol', (select id from store where property='EC1N 2RN'))

有些牙套不见了。每个数据集都必须用大括号和 SELECT 语句括起来。

我不知道你的表结构,但也许还有另一个错误:第一个数据集由 postcode 列过滤,第二个数据集由 property 列过滤...

关于postgresql - Postgres : insert value from another table as part of multi-row insert?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57186062/

25 4 0