PostgreSQL:意外更新数据库中的所有记录

问题描述 投票:0回答:1

我有一张产品表和一张临时表。 任务:使用 temp 的信息更新 prod 表中的所有记录。

update prod set status = 'on'
  from prod pd
  join temp tm using (factory_id) 
where pd.status = 'off'

不幸的是,该代码访问了 prod 表中设置为“on”的所有记录,无论它们是否处于临时状态。

你能解释一下这是怎么发生的吗?我认为只有状态为“关闭”的记录才必须生效。 请求中应该更正哪些内容才能使其正确?

postgresql sql-update
1个回答
1
投票

发生这种情况是因为

from
,您应该提及
prod
一次:

update prod set status = 'on'
  from temp tm 
where prod.status = 'off'
and prod.factory_id = tm.factory_id;
© www.soinside.com 2019 - 2024. All rights reserved.