我有两张桌子:
表A:
ID NAME
1 First
2 Second
3 Second
4 Third
5 First
表B(引用表A记录):
A_ID REC
1 10
2 10
3 20
4 20
5 20
3 30
表A有2个重复记录,我可以轻松删除,但我需要用较少的重复记录ID更新表B.我想最终得到:
A_ID REC
1 10
2 10
2 20
4 20
1 20
2 30
我已经为MySQL解决了这个问题,但由于Oracle不支持UPDATE语句中的连接,因此卡在了Oracle上。
答案 0 :(得分:1)
您可以将此作为查询执行。首先,获得新的id:
select a1.id, min(a2.id) as min_id
from a a1 left join
a a2
on a1.name = a2.name
group by a1.id;
然后,作为查询:
select b.*, a.min_id
from b join
(select a1.id, min(a2.id) as min_id
from a a1 left join
a a2
on a1.name = a2.name
group by a1.id
) a
on b.a_id = a.id;
如果你真的需要更新,那有点棘手:
update b
set b.a_id = (select min(a2.id) as min_id
from a a1 left join
a a2
on a1.name = a2.name
where a1.id = b.a_id
)
where b.id <> (select min(a2.id) as min_id
from a a1 left join
a a2
on a1.name = a2.name
where a1.id = b.a_id
)