使用 psycopg2 更新 SET 语句中的 1 个以上的列



我尝试根据在另一个表中找到的值更新一个表。以下作品:

UPDATE table1 SET col1 = ( SELECT col1 from table2 WHERE table2.col1 = table1.col1 );

我想使用几列做同样的事情。我认为以下内容应该会带来预期的结果">

UPDATE table1 SET (col1, col2) = ( SELECT col1, col2 from table2 WHERE table2.col1 = table1.col1 );

但我得到一个

syntax error at or near "SELECT"
LINE 1: UPDATE table1 SET (col1, col2) = ( SELECT col1, col2 f...

任何帮助表示赞赏。

这应该有效:

update table1 t1
set col1 = t2.col1, 
  col2 = t2.col2
from table2 t2
where t1.col1 = t2.col1;
  • SQL 小提琴演示

话虽如此,无需更新col1 = t2.col1因为这是您的join标准。

最新更新