1

我有以下交易:

BEGIN;

    SELECT t1.a, t2.b
      FROM table1 t1
INNER JOIN table2 t2 ON (t1.c = t2.c)
     WHERE t1.cond;

     UPDATE table1
        SET d = 'Value'
      WHERE cond;

COMMIT;

当我使用相同的条件更新同一个表时,我可以执行以下操作,放弃对事务的需要:

   UPDATE table1
      SET d = 'Value'
    WHERE cond
RETURNING a;

这样,我会b失去table2. 我尝试了以下方法:

    SELECT t1.a, t2.b
      FROM table1 t1
INNER JOIN table2 t2 ON (t1.c = t2.c)
     WHERE t1.a IN (
                       UPDATE table1
                          SET d = 'Value'
                        WHERE cond
                    RETURNING a
                   );

和:

    SELECT t1.a, t2.b
      FROM table1 t1
INNER JOIN table2 t2 ON (t1.c = t2.c)
INNER JOIN (
               UPDATE table1
                  SET d = 'Value'
                WHERE cond
            RETURNING a
           ) u ON (t1.a = u.a);

这两个都产生了语法错误(第一个抱怨UPDATE,第二个抱怨SET)。

对此的任何意见将不胜感激。

4

1 回答 1

0

像这样的东西:

update table1 as t1
  SET d = 'Value'
from table2 as t2
where t1.c = t2.c
  and ...condition...
returning t1.a, t2.b;
于 2013-08-05T20:52:24.623 回答