Home >Database >Mysql Tutorial >Oracle 两个表之间更新的实现

Oracle 两个表之间更新的实现

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-07 17:25:49894browse

Oracle中,如果跨两个表进行更新,Sql语句写成这样,Oracle 不会通过。查了资料,Sql语句需要这样写才行

前提条件:

表info_user中有字段id和name,字段id为索引

表data_user_info中有字段id和name,字段id为索引

其中表info_user中字段id和表data_user_info中字段id数值一致。

要求实现:

更新表info_user中的字段name 与表data_user_info中字段name一致。

实现的SQL语句为:

update info_user i set (i.name) = (select  d.name from data_user_info d where d.id = i.id)

where i.id = (select d.id from data_user_info d where d.id = i.id);

commit;

-------------------------分割线-------------------------

Oracle中,如果跨两个表进行更新,Sql语句写成这样

Update Table a set a.ID=Table2.ID where a.Name = Table2.Name

  Oracle 不会通过。查了资料,,Sql语句需要这样写才行

Update Table a set a.ID=(select b.ID from Table2 b where b.Name = a.Name ) where exist (select 1 from Table2 b.Name=a.Name)

  更新多个字段也可以

Update Table a set a.ID=(select b.ID from Table2 b where b.Name = a.Name ),a.Code=(select b.Code from Table2 b where b.Name = a.Name ) where exist (select 1 from Table2 b.Name=a.Name)

linux

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Oralce swap 100%案例分析Next article:使用RMAN进行备份