ORACLE 多表关联update
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
No related posts.