我正在处理复杂的问题,但我会将其简化为此问题。
我有两张桌子
A [ID, column1, column2, column3]
B [ID, column1, column2, column3, column4]
C [ID, column1, column2,column3]
我正在使用此查询创建另一个第三个表。
UPDATE C
set column1=t.firstTab, column2=t.secondTab, column3=t.thirdTab
from (select A.column1 as firstTab, B.column2 as secTab,
(A.column1 + B.column2) thirdTab
from A, B limit 1; ) as t ;
我得到了:更新0
当我运行此查询时:
select A.column1 as firstTab, B.column2 as secTab, (A.column1 + B.column2) thirdTab
from A, B limit 1;
我得到了结果。
答案 0 :(得分:1)
根据评论,这将更新投影的第一行(未指定顺序)的所有C行:
UPDATE C
set column1=t.firstTab, column2=t.secTab, column3=t.thirdTab
FROM
(select A.column1 as firstTab, B.column2 as secTab,
(A.column1 + B.column2) thirdTab
from A, B limit 1) as t;
如果您只想使用投影的第一行更新一行C,并且C.ID是主键:
UPDATE C
set column1=t.firstTab, column2=t.secondTab, column3=t.thirdTab
from
(select A.column1 as firstTab, B.column2 as secondTab,
(A.column1 + B.column2) thirdTab
from A, B limit 1) as t
WHERE C.ID IN (SELECT ID from C LIMIT 1);
但是,如果您在C上没有主键,则可以使用CTID system column:
UPDATE C
set column1=t.firstTab, column2=t.secondTab, column3=t.thirdTab
from
(select A.column1 as firstTab, B.column2 as secondTab,
(A.column1 + B.column2) thirdTab
from A, B limit 1) as t
WHERE CTID IN (SELECT CTID from C LIMIT 1);