使用psycopg2在UPDATE SET语句中更新超过1 db的列

时间:2013-10-07 22:42:12

标签: sql postgresql python-3.x psycopg2

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

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...

任何帮助表示感谢。

1 个答案:

答案 0 :(得分:0)

这应该有效:

update table1 t1
set col1 = t2.col1, 
  col2 = t2.col2
from table2 t2
where t1.col1 = t2.col1;

据说,无需更新col1 = t2.col1,因为那是您的join条件。