[英]postgresql 9.6:issue with UPDATE
I'm stuck with an UPDATE, probably I'm messing something up: 我坚持使用UPDATE,可能是我搞砸了:
the amount of rows in the ' proj_los
' table expected to be updated are 32, but only 8 get updated, unless I add an AND condition. 除非要添加AND条件,否则“
proj_los
”表中预期要更新的行数为32,但是只有8个被更新。
select count(*) from import.tmp_kk where root_id = '57b2e67b-5862-499a-a471-0f2f6b23440e';
> 32
1) correct result -> using last AND clause ( root_id = '57b2e67b-5862-499a-a471-0f2f6b23440e'
) 1)正确的结果->使用最后的AND子句(
root_id = '57b2e67b-5862-499a-a471-0f2f6b23440e'
)
sql> update
proj_los as oko
set lo_root_id = import.tmp_kk.root_id
from import.tmp_kk
where oko.lo_id = import.tmp_kk.lo_id
and import.tmp_kk.root_id = '57b2e67b-5862-499a-a471-0f2f6b23440e'
[2018-10-05 18:13:59] 32 rows affected in 50 ms
select count(*) from proj_los where lo_root_id = '57b2e67b-5862-499a-a471-0f2f6b23440e';
> 32
2) wrong result 2)错误的结果
sql> update
proj_los as oko
set lo_root_id = import.tmp_kk.root_id
from import.tmp_kk
where oko.lo_id = import.tmp_kk.lo_id;
[2018-10-05 18:17:31] 174202 rows affected in 17 s 427 ms
select count(*) from proj_los where lo_root_id = '57b2e67b-5862-499a-a471-0f2f6b23440e';
> 8
Any help on this? 有什么帮助吗?
You hit the problem described in the postgres documentation of UPDATE ... FROM ...
: 您遇到了
UPDATE ... FROM ...
的postgres 文档中描述的问题:
When using FROM you should ensure that the join produces at most one output row for each row to be modified.
使用FROM时,应确保该联接为要修改的每一行最多产生一个输出行。 In other words, a target row shouldn't join to more than one row from the other table(s).
换句话说,目标行不应与其他表的多个行连接。 If it does, then only one of the join rows will be used to update the target row, but which one will be used is not readily predictable
如果是这样,那么将仅使用联接行之一来更新目标行,但是将很难预测将使用哪一行。
If the additional filter by root_id
is not specified the query on which the update is based upon most probably returns multiple rows for every updated row. 如果未指定
root_id
的附加过滤器,则更新所基于的查询很可能为每个更新的行返回多个行。 Some unpredictable row is used to do the update. 一些不可预测的行用于执行更新。 And what must have happened is that in many cases incorrect
root_id
had been used. 而且必须发生的是,在许多情况下,使用了错误的
root_id
。
Note how update modified 174202
rows (and not 8 rows as you written). 请注意如何更新修改的
174202
行(而不是您编写的8行)。 It updated root_id
s in unrelated records that you do not want to be updated. 它更新了
root_id
更新的不相关记录中的root_id
。
So check import.tmp_kk
table. 因此,请检查
import.tmp_kk
表。 It contains more then one record (with different root_id
s) for given lo_id
. 对于给定的
lo_id
它包含多于一条的记录(具有不同的root_id
)。
I dont understand: I said that the correct result would be an update or 32 rows instead of 8 for that root_id. 我不明白:我说正确的结果将是该root_id的更新或32行而不是8行。
of course import.tmp_kk
contains more than a lo_id
for a single root_id
, on the target table proj_los
, lo_root_id
will be a FK for lo_id
. 当然
import.tmp_kk
包含比更lo_id
单个root_id
,对目标表proj_los
, lo_root_id
将是一个FK的lo_id
。
For what concern the postgres docs, to me it seems that it says essentially to make sure that there's no dupes in the result joined table: 对于postgres文档所关心的问题,在我看来,它基本上是在确保结果联接表中没有重复对象:
"When using FROM you should ensure that the join produces at most one output row for each row to be modified." “使用FROM时,应确保该联接为要修改的每一行最多产生一个输出行。”
so I checked with: 所以我检查了:
with tmp_joinres as (
select oko.lo_id, gg.root_id
from proj_los oko
full outer join import.tmp_kk gg on oko.lo_id = gg.lo_id
)
select tmp_joinres.lo_id, tmp_joinres.root_id, count(*)
from tmp_joinres
group by tmp_joinres.lo_id, tmp_joinres.root_id
having count(*) > 1;
but there's no results: 但没有结果:
[2018-10-05 21:58:20] 0 rows retrieved in 5 s 737 ms (execution: 5 s 729 ms, fetching: 8 ms)
Can you or someone else please tell me how to modify the UPDATE statement in order to achieve the correct result? 您或其他人可以告诉我如何修改UPDATE语句以获得正确的结果吗?
Thanks 谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.