oracle中的update语句能用相关子查询么?

2025-03-23 09:48:40
推荐回答(2个)
回答1:

update table1 a set a.name = (select b.name from table2 where a.id=b.id and rownum=1 );

其实要按你的具体需求而定。

步骤多,但效率比较高:
1、create table 临时表 value (select a.id,a.name,b.name,... from table1 a,table2 b where a.id=b.id)
2、删除table1中的记录,不要drop
3、insert into table1 select 你需要的字段 from 临时表。

回答2:

因为查出的值很多,不知道你要更新为哪个,要是值都一样的话,用最大值或最小值,这样出来的是一条记录
update table1 a set a.name = (select max(b.name) from table2 where a.id=b.id);
where ---你可以在后面再次限制更新的记录条件