更新多个值postgres

时间:2019-12-02 12:21:40

标签: sql postgresql

当我尝试使用此请求更新多行时遇到问题

update redux.rentalpivots tg
set (maxleasedatebymonth) = (select a.maxleasedatebymonth 
                             from redux.rental_latest_lease a,
                                  redux.rentalpivots b 
                             where a.surfaceid = b.surfaceid
                               and a.startdate = b.startdate
                               and b.enabled = TRUE 
                               and a.maxleasedatebymonth = b.leasestartdate
                               and tg.surfaceid = b.surfaceid
                               and tg.startdate = b.startdate 
                               and tg.enabled = b.enabled and b.surfaceid =  ?  )
 WHERE surfaceid = ?

返回:

  

org.postgresql.util.PSQLException:错误:返回了多行   由用作表达式的子查询

2 个答案:

答案 0 :(得分:0)

您可能打算关联一个子查询。我不确定这是否可以解决您的问题,但查询应该看起来像这样:

update redux.rentalpivots tg
    set maxleasedatebymonth = (select a.maxleasedatebymonth 
                               from redux.rental_latest_lease rll 
                               where rll.surfaceid = tg.surfaceid and
                                     rll.startdate = tg.startdate and
                                     rll.maxleasedatebymonth = tg.leasestartdate
                              )
     where tg.enabled and tg.surfaceid = ?;

如果仍然有问题,可以通过向子查询中添加limit 1来解决此问题。

也就是说,如果您认为只应匹配一行,而多行则匹配,则应找出原因并修复数据。

答案 1 :(得分:0)

您可能正在寻找UPDATE FROM ..语法,例如:

update redux.rentalpivots tg
  set maxleasedatebymonth = x.maxleasedatebymonth
from redux.rental_latest_lease a
where tg.surfaceid = a.surfaceid
  and tg.startdate = a.startdate 
  and tg.leasestartdate = a.maxleasedatebymonth 
  and tg.enabled
  and tg.surfaceid = ?;
相关问题