连接表时数据库 UPDATE SET,由于笛卡尔积,要更新的值不是唯一的

Database UPDATE SET when joining tables and the value to update is not unique due to cartesian product

当 SET UPDATE 时,PostgreSQL(也许标准 SQL 考虑到了这一点)如何表现 不是唯一记录而是笛卡尔积?

想象一下,出于某种原因,b table 包含: (1,1),(1,1),(1,2) 要更新的值应该是多少(或者数据库是否生成笛卡尔积或创建记录或其他内容)?

UPDATE table_a a
SET b_value = b.value
FROM (SELECT id, value FROM mdm.table_b) AS b
WHERE b.a_id = a.id;

您的查询不稳定。可能会发生不好的事情。

documentation 很清楚:

When using FROM you should ensure that the join produces at most one output row for each row to be modified. 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.

然后:

Because of this indeterminacy, referencing other tables only within sub-selects is safer, though often harder to read and slower than using a join.

如果您要遵循文档的建议,您可以将查询表述为:

update table_a a
set b_value = (select max(b.value) from table_b b where b.a_id = a.id)
where exists (select 1 from table_b b where b.a_id = a.id)

子查询中的聚合函数确保返回单行(您也可以使用 min())。你可以也可以用from来表达:

update table_a a
set b_value = b.value
from (select a_id, max(value) as value from table_b group by a_id) as b
where b.a_id = a.id;