首页 > 数据库 > oracle关联update语句

oracle关联update语句

2010年10月30日 亲亲宝宝 发表评论 阅读评论

Oracle两表关联执行update时,因为没有像SqlServer的update from,因此要麻烦一些,通常有以下四种方式:

第一种:更新的条件为两个表的查询关联

update customers  a       — 使用别名

set    customer_type=’01’ –01 为vip,00为普通

where  exists (select 1

from   tmp_cust_city b

where  b.customer_id=a.customer_id

)

第二种:更新的条件为两个表的查询关联,值为查询值

update customers a   — 使用别名

set    city_name=(select b.city_name from tmp_cust_city b where b.customer_id=a.customer_id)

where  exists (select 1

from   tmp_cust_city b

where  b.customer_id=a.customer_id

)

第三种: update 超过2个值

update customers a   — 使用别名

set    (city_name,customer_type)=(select b.city_name,b.customer_type

from   tmp_cust_city b

where  b.customer_id=a.customer_id)

where  exists (select 1

from   tmp_cust_city b

where  b.customer_id=a.customer_id

)

第四种,使用视图方式更新,这样能避免对B表或其索引的2次扫描,但前提是 A(customer_id) b(customer_id)必需是unique index

或primary key。否则报错:

update (select a.city_name,b.city_name as new_name

from   customers a,

tmp_cust_city b

where  b.customer_id=a.customer_id

)

set    city_name=new_name

分类: 数据库 标签: 4,236 次阅读
原文链接:http://www.wenhq.com/article/view_660.html
欢迎转载,请注明出处:亲亲宝宝
  1. 2010年11月17日16:25 | #1

    虽然看不懂是什么。。。但是支持一下。。。

    [回复]

  2. 2011年1月18日16:56 | #2

    还是不明白,支持一下!

    [回复]

  1. 2010年10月31日11:31 | #1