ORACLE 多表关联update

本文介绍了Oracle中处理两表关联执行update操作的四种方法,包括使用别名、查询值更新、多值更新及视图方式,旨在简化复杂操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值