Postgresql:更新问题

时间:2010-09-05 10:13:32

标签: sql postgresql

我想做一些更新,逻辑上看起来如下 -

UPDATE    t1 
SET       (x1, x2) = (SELECT  x1, x2 
                              FROM    t1 
                              WHERE id_1 = 8 
                              AND id_2 =1 
                              AND id_3 = 3)
;

但是从documentation看来,这些类型的更新目前还没有在Postgresql中实现。除了经典风格之外,是否还有针对此问题的解决方法(按经典我的意思是set x1 = value1, x2=value2,.....样式)?

1 个答案:

答案 0 :(得分:3)

你可以这样做:

UPDATE t1 SET x1 = a.x1, x2 = a.x2 FROM 
   (SELECT x1, x2 FROM t1 WHERE id_1 = 8 AND id_2 = 1 AND id_3 = 3) AS a

可能需要附加条件:

UPDATE t1 SET x1 = a.x1, x2 = a.x2 FROM 
   (SELECT id, x1, x2 FROM t1 WHERE id_1 = 8 AND id_2 = 1 AND id_3 = 3) AS a 
   WHERE a.id = t1.id