你好,游客 登录 注册 搜索
背景:
阅读新闻

Oracle 两个表之间更新的实现

[日期:2013-01-23] 来源:Linux社区  作者:xuhuan_wh cglNet [字体:大 中 小]

前提条件:

表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
相关资讯       Oracle 两表更新  Oracle 更新 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款