我正在创建一个分析输入的过程json数据并存储在表中。函数如下:, s8 }5 q; J4 ^# y
create or replace function test_func(d json)returns void as $$ begin with n as ( insert into t1 (name) values (d::json -> 'name') returning id ),c as ( insert into t2 (cars) values json_array_elements_text(d::json -> 'cars') returning id ) insert into t3 (id,name_id,cars_id,brand) select 1,n.id,c.id,json_array_elements_text(d::json -> 'brands') from n,c;end;$$ language plpgsql;CREATE TABLE t1( "id" SERIAL PRIMARY KEY, "name" text NOT NULL)CREATE TABLE t2( "id" SERIAL PRIMARY KEY, "cars" text NOT NULL, "car_type" int)CREATE TABLE t3( "id" int, "name_id" int REFERENCES t1(id), "cars_id" int REFERENCES t2(id), "brand" text)数据输入的名称是文本,汽车和品牌是数组,包装在json所以最后一个插入有混合值类型,如果这个人有两辆车,我会在那里t因为c.id和json_array_elements_text(d- Y% l: e3 n3 | Q1 R
:: json->’brands’)有两个数据集,即2x2 = 4,如何一对一映射插入值?所以第一个c.id应映射到第一个品牌。 ; i, f! W: g" q# {( X, x1 b , R0 F& l3 S# e" D) |9 i8 { 解决方案: