我只是从Postgres
开始,然后发现自己在Query
中制作了以下Postgres
,旨在从4个不同的tables
中获取数据。
SELECT
table1.id,
table2.icon,
table3.title,
table4.description
FROM
table1
JOIN table5 ON id_t5 = id_t3
JOIN table3 ON id_t1 = id_t3
AND id_t3 = 816
LEFT JOIN table2 ON table5.id_t2_fk = table2.id_t2
LEFT JOIN table4 ON table4.id_t3_fk = table1.id_t1;
我的问题是,在生成UPDATE
之后,我必须在这4个表中创建一个Query
。
我想不出如何解决问题,因为UPDATE
的{{1}}语法与Postgres
或MySQL
的语法不同。
我试图这样做:
SQLserver
答案 0 :(得分:0)
Postgres允许您在CTE中进行更新。也许这就是您想要的:
with data as (
select t1.id, t2.id_t2, t2.icon, t3.id_t3, t3.title,
t4.id_t4, t4.description
from table1 t1 join
table5 t5
on id_t5 = id_t3 join
table3
on id_t1 = id_t3 and id_t3 = 816 left join
table2 t2
on t5.id_t2_fk = t2.id_t2 left join
table4 t4
on t4.id_t3_fk = t1.id_t1
where t1.id_t1= 816
),
u2 as (
update table2
set icon = 'new icon'
where t2.id_t3 in (select data.id_t2 from data)
returning *
),
u3 as (
update table3
set title = 'new title'
where id_t3 in (select data.id_t3 from data)
returning *
)
update table4
set description = 'new description'
where id_t4 in (select data.id_t4 from data);
否则,类似的事情。