开发者

Postgresql: Update problem

I want to do some updates, which logically looks like below -

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

But from the docum开发者_如何学Pythonentation it looks like these types of updates are currently not implemented in Postgresql. Is there any workaround for this problem other than the classic style(By classic I meant set x1 = value1, x2=value2,..... style)?


You can do it:

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

Aditional condition may be required:

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜