Updating and join on multiple rows, which row#39;s value is used?(在多行上更新和连接,使用哪一行的值?)
问题描述
假设我有以下语句,内部连接产生 3 行,其中 a.Id = b.Id,但 3 行中的每一行都有不同的 b.Value.由于 tableA 只更新了一行,所以更新时使用了 3 个值中的哪一个?
Let's say I have the following statement and the inner join results in 3 rows where a.Id = b.Id, but each of the 3 rows have different b.Value's. Since only one row from tableA is being updated, which of the 3 values is used in the update?
UPDATE a
SET a.Value = b.Value
FROM tableA AS a
INNER JOIN tableB as b
ON a.Id = b.Id
推荐答案
我认为这种情况没有规则,你不能依赖特定的结果.
I don't think there are rules for this case and you cannot depend on a particular outcome.
如果您在特定行之后,比如最新的一行,您可以使用 apply
,例如:
If you're after a specific row, say the latest one, you can use apply
, like:
UPDATE a
SET a.Value = b.Value
FROM tableA AS a
CROSS APPLY
(
select top 1 *
from tableB as b
where b.id = a.id
order by
DateColumn desc
) as b
这篇关于在多行上更新和连接,使用哪一行的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在多行上更新和连接,使用哪一行的值?


- 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01
- 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01
- 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01
- 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01
- 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01
- SQL 临时表问题 2022-01-01
- 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01
- 更改自动增量起始编号? 2021-01-01
- 导入具有可变标题的 Excel 文件 2021-01-01
- 在SQL中,如何为每个组选择前2行 2021-01-01