Sql 更新多值postgres
当我尝试使用此请求更新多行时,出现问题Sql 更新多值postgres,sql,postgresql,Sql,Postgresql,当我尝试使用此请求更新多行时,出现问题 update redux.rentalpivots tg set (maxleasedatebymonth) = (select a.maxleasedatebymonth from redux.rental_latest_lease a, redux.rentalpivots b
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:错误:返回多行
由用作表达式的子查询执行
您可能想要一个相关的子查询。我不确定这是否能解决您的问题,但查询应该更像这样:
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
来解决此问题
这就是说,如果您认为只有一行应该匹配,而多行应该匹配,那么您应该找出原因并修复数据。您可能正在寻找来自..的
更新语法,例如:
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 = ?;