如何基于子查询进行更新?
How to update based on subquery?
我有两个table:
Product
Id
Code
Value
Info
Id
Serial
Description
现在我想用 table Info
中 Serial
的值更新 table Product
中的每一列 Code
。我的 where 条件很方便,因为我所有的 Product
code
目前都是 Id
来自 Info
.
这就是我正在尝试的:
update Product P set
P.Code = (select Serial from Info where id = P.Code);
我在这里收到错误:ORA-01427: single-row subquery returns more than one row
。
我觉得我少了一个 where 子句,但我不确定把它放在哪里?
您的子查询 returns 多个结果(如错误所述)。
您可以使用以下聚合函数来限制更新的行:
update Product P set P.Code = (select MAX(Serial) from Info where id = P.Code);
我有两个table:
Product |
---|
Id |
Code |
Value |
Info |
---|
Id |
Serial |
Description |
现在我想用 table Info
中 Serial
的值更新 table Product
中的每一列 Code
。我的 where 条件很方便,因为我所有的 Product
code
目前都是 Id
来自 Info
.
这就是我正在尝试的:
update Product P set
P.Code = (select Serial from Info where id = P.Code);
我在这里收到错误:ORA-01427: single-row subquery returns more than one row
。
我觉得我少了一个 where 子句,但我不确定把它放在哪里?
您的子查询 returns 多个结果(如错误所述)。 您可以使用以下聚合函数来限制更新的行:
update Product P set P.Code = (select MAX(Serial) from Info where id = P.Code);