将列值从一个表更新到另一个表

时间:2017-01-20 13:30:27

标签: postgresql

我有三张桌子

X [id,event_time]
Y [x_id,z_id] -- here x_id is id from X and z_id is id from Z
Z [id,event_time]

现在我想用Z [event_time]更新X [event_time]的值。是否有任何可以形成的查询?

3 个答案:

答案 0 :(得分:1)

类似这样的事情

UPDATE X
SET event_time = z.event_time
FROM Z z
JOIN Y y ON y.z_id = z.id
JOIN X x ON y.x_id = x.id

答案 1 :(得分:1)

UPDATE X
SET event_id = (SELECT z.event_id FROM Z INNER JOIN Y WHERE Z.id=Y.z_id and Y.x_id=X.id)

答案 2 :(得分:1)

update x
set x.event_time = t.event_time
from (
    select z.event_time
        ,y.x_id
    from z
    join y on z.id = y.z_id
    ) t
where x.id = t.x_id