[英]Update a table use result of another query in Postgres
I have a 2 table like this:我有一个像这样的 2 表:
table1 : ( time, value, id ), table2 :( time, value, id, ... ) table1 :(时间,值,id ), table2 :(时间,值,id,... )
I need to update table1 with a result of a query on table2 based on id , for example the query can be:我需要使用基于id对table2进行查询的结果来更新table1 ,例如查询可以是:
SELECT * from table2 where value > 2
and this query returns more than hundreds of rows,这个查询返回超过数百行,
I need update table1
with these rows based on id
( set time=q.time, value=q.value where id=q.id
) is it possible with sql query?我需要根据
id
( set time=q.time, value=q.value where id=q.id
) 用这些行更新table1
是否可以使用 sql 查询?
I don't need UPSERT as I'm sure I have same id in both tables, just need update我不需要 UPSERT,因为我确定两个表中的 id 相同,只需要更新
SELECT *
into temp buffer
from table2
where value > 2;
insert into table1(list of columns to be updated)
select (list of columns to be selected)
from buffer;
NOTE___ --buffer is a temporary table that stores the output of the main query -- make sure both tables (table1 and table2) have the same columns and are arranged in the same order NOTE___ --buffer 是一个临时表,用于存储主查询的 output -- 确保两个表(table1 和 table2)具有相同的列并且以相同的顺序排列
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.