任何人都可以帮我解决这个问题
表名:RW_LN
LN_ID RE_LN_ID RE_PR_ID
LN001 RN001 RN002
LN002 RN002 RN003
LN003 RN003 RN001
LN004 RN001 RN002
我的更新查询是:
update table RW_LN set RE_LN_ID=(
select LN_ID
from RW_LN as n1,RW_LN as n2
where n1.RE_LN_ID = n2.RE_PR_ID)
我的预期结果是:
LN_ID RE_LN_ID
LN001 LN003
LN002 LN004
LN003 LN002
LN004 LN003
上面的查询显示错误为SUB QUERY RETURNS MULTIPLE ROWS
。任何人都可以为此提供解决方案,我是Oracle 9i中的初学者。所以坚持逻辑
答案 0 :(得分:0)
您可以尝试使用distinct
update table RW_LN set RE_LN_ID=(
select distinct LN_ID
from RW_LN as n1,RW_LN as n2
where n1.RE_LN_ID = n2.RE_PR_ID)
如果仍然返回多行,则意味着您在此过程中某处缺少连接,或者可能存在需要使用主键的错误架构。
答案 1 :(得分:0)
猜猜,但可能这就是你想要的。
update
RW_LN n1
set
RE_LN_ID=(
select n2.LN_ID
from RW_LN n2
where n1.RE_LN_ID = n2.RE_PR_ID)
where exists (
select null
from RW_LN n2
where n1.RE_LN_ID = n2.RE_PR_ID and
n2.ln_id is not null)
目前,您正在更新的行与子查询中返回的值之间没有相关性。
查询内容如下:
For every row in RW_LN change the value of RE_LN_ID to be:
the value of LN_ID in a row in RW_LN for which:
the RE_PR_ID equals the original tables value of RE_LN_ID
IF there exists at least one row in RW_LN for which:
RE_PR_ID is the same as RE_LN_ID in the original table AND
LN_ID is not null
答案 2 :(得分:0)
如果你想采取“最大”的相应LN_ID,你可以做
update RW_LN r1
set r1.RE_LN_ID = (select MAX(LN_ID)
FROM RW_LN r2
where r1.RE_LN_ID = r2.RE_PR_ID);
请参阅SqlFiddle
但你应该解释为什么你选择(作为新的RE_LN_ID)LN004而不是LN001用于LN_ID LN002(因为你可以选择两者)