ORACLE 多表关联 UPDATE 语句
两表(多表)关联update -- 仅在where字句中的连接
--直接赋值
update customers a -- 使用别名
set customer_type='01'
where exists (select 1
from tmp_cust_city b
where b.customer_id=a.customer_id
)
两表(多表)关联update -- 被修改值由另一个表运算而来
-- update 1个值
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
)
其中where exists (select 1
from tmp_cust_city b
where b.customer_id=a.customer_id
)
很重要。因为前面的update语句,update的是全表,如果(select b.city_name from tmp_cust_city b where b.customer_id=a.customer_id)select值存在,会update,如果值不存在,就会update成null,不符合需求,所以,要加上exists语句,这样,就只会update存在的记录,当然,也可以使用nvl函数。