【问题标题】:PostgreSQL update column with JOIN over 3 tables使用 JOIN 超过 3 个表的 PostgreSQL 更新列
【发布时间】:2016-07-27 13:33:47
【问题描述】:

我有 3 个表,我想用来自table2 的信息(列status)更新table1(列status)。 table1table2 之间的链接在表中 table_con

http://www.sqlfiddle.com/#!15/6ce460/4

我想到了一个join 并使用join 的结果来更新table1

select t1.status as t1status,t2.status as t2status,t1.p_id as t1pid, t2.x_id as t2xid
   from table1 t1
      JOIN table_con tc
      ON t1.p_id = tc.p_id

      JOIN table2 t2 
      ON t2.x_id = tc.x_id;

join 到目前为止有效,但我不知道如何继续, 并且查询应该在 psql 中工作。谢谢

【问题讨论】:

  • 您要更新哪些列?

标签: sql postgresql join sql-update


【解决方案1】:

在 Postgres 中,您可以在 update 语句中表达联接:

update table1 t1 
   set ?? = ??
   from table_con tc join
        table2 t2 
        on t2.x_id = tc.x_id
   where t1.p_id = tc.p_id;

用您想设置的列和值填写set 列。

【讨论】:

  • 如果 t1 必须同时与 tc 和 t2 连接怎么办?
  • @AMJ。 . .然后,您将在 WHERE 子句中添加另一个条件。
猜你喜欢
  • 2020-08-02
  • 2013-03-25
  • 2018-06-23
  • 1970-01-01
  • 2021-01-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-08
相关资源
最近更新 更多