动态 Insert/update 基于 postgresql 中的 where 子句

Dynamic Insert/update based on where clause in postgresql

我有两个table如下图

ob_period

person_id     ob_start_date

  1            2007/02/11
  2            2008/05/13  
  3            2008/07/29
  4            2006/03/21

访问

person_id     visit_date

  1            2003/06/21
  2            2005/02/23  
  3            2006/04/19
  5            2004/06/11

我想用 visit_table

的 "visit_date" 更新 "ob_period" table 的 "ob_start_date"

我正在尝试类似下面的更新,但它无法工作,因为我不确定如何使用另一个 table

的动态值进行更新
update ob_period a
set a.ob_start_date = b.visit_date
where a.person_id = b.person_id

我希望我的输出如下所示

输出

person_id     visit_date

  1            2003/06/21
  2            2005/02/23  
  3            2006/04/19
  4            2006/03/21

你能帮我解决这个问题吗?

您需要 table 引用第二个 table:

update ob_period p
    set ob_start_date = v.visit_date
    from visit v
    where p.person_id = v.person_id

试试这个:

 update ob_period a
    set a.ob_start_date = b.visit_date
    from visit b
    where a.person_id = b.person_id