溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

mysql實(shí)現(xiàn)多表關(guān)聯(lián)更新的詳細(xì)方法

發(fā)布時(shí)間:2020-06-08 11:13:44 來(lái)源:PHP中文網(wǎng) 閱讀:843 作者:三月 欄目:MySQL數(shù)據(jù)庫(kù)

不知道大家之前對(duì)類(lèi)似mysql實(shí)現(xiàn)多表關(guān)聯(lián)更新的詳細(xì)方法的文章有無(wú)了解,今天我在這里給大家再簡(jiǎn)單的講講。感興趣的話(huà)就一起來(lái)看看正文部分吧,相信看完mysql實(shí)現(xiàn)多表關(guān)聯(lián)更新的詳細(xì)方法你一定會(huì)有所收獲的。                                                            

mysql實(shí)現(xiàn)多表關(guān)聯(lián)更新的詳細(xì)方法

創(chuàng)建以下的簡(jiǎn)單模型,并構(gòu)造部分測(cè)試數(shù)據(jù):
在某個(gè)業(yè)務(wù)受理子系統(tǒng)BSS中,

--客戶(hù)資料表
create table customers
(
customer_id number(8) not null, -- 客戶(hù)標(biāo)示
city_name varchar2(10) not null, -- 所在城市
customer_type char(2) not null, -- 客戶(hù)類(lèi)型
...
)
create unique index PK_customers on customers (customer_id)

由于某些原因,客戶(hù)所在城市這個(gè)信息并不什么準(zhǔn)確,但是在客戶(hù)服務(wù)部的CRM子系統(tǒng)中,通過(guò)主動(dòng)服務(wù)獲取了部分客戶(hù)20%的所在城市等準(zhǔn)確信息,于是你將該部分信息提取至一張臨時(shí)表中:

create table tmp_cust_city
(
customer_id number(8) not null,
citye_name varchar2(10) not null,
customer_type char(2) not null
)

1) 最簡(jiǎn)單的形式

--經(jīng)確認(rèn)customers表中所有customer_id小于1000均為'北京'
--1000以?xún)?nèi)的均是公司走向全國(guó)之前的本城市的老客戶(hù):)
update customers
set city_name='北京'
where customer_id<1000

2) 兩表(多表)關(guān)聯(lián)update -- 僅在where字句中的連接

--這次提取的數(shù)據(jù)都是VIP,且包括新增的,所以順便更新客戶(hù)類(lèi)別
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
)

3) 兩表(多表)關(guān)聯(lián)update -- 被修改值由另一個(gè)表運(yùn)算而來(lái)

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 超過(guò)2個(gè)值
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
)

注意在這個(gè)語(yǔ)句中,

(select b.city_name,b.customer_type
from tmp_cust_city b
where b.customer_id=a.customer_id
)
與
(select 1
from tmp_cust_city b
where b.customer_id=a.customer_id
)

是兩個(gè)獨(dú)立的子查詢(xún),查看執(zhí)行計(jì)劃可知,對(duì)b表/索引掃描了2篇;

如果舍棄where條件,則默認(rèn)對(duì)A表進(jìn)行全表

更新,但由于(select b.city_name from tmp_cust_city b where where b.customer_id=a.customer_id)

有可能不能提供"足夠多"值,因?yàn)閠mp_cust_city只是一部分客戶(hù)的信息,

所以報(bào)錯(cuò)(如果指定的列--city_name可以為NULL則另當(dāng)別論):

01407, 00000, "cannot update (%s) to NULL"

// *Cause:

// *Action:

替換的方法:

update customers a -- 使用別名
set city_name=nvl((select b.city_name from tmp_cust_city b where b.customer_id=a.customer_id),a.city_name)
或者
set city_name=nvl((select b.city_name from tmp_cust_city b where b.customer_id=a.customer_id),'未知')
-- 當(dāng)然這不符合業(yè)務(wù)邏輯了

一個(gè)比較簡(jiǎn)便的方法就是將A表代入 值表達(dá)式 中,使用group by 和having 字句查看重復(fù)的紀(jì)錄。

(select b.customer_id,b.city_name,count(*)
 from tmp_cust_city b,customers a 
 where b.customer_id=a.customer_id
 group by b.customer_id,b.city_name
 having count(*)>=2
)

看完mysql實(shí)現(xiàn)多表關(guān)聯(lián)更新的詳細(xì)方法這篇文章,大家覺(jué)得怎么樣?如果想要了解更多相關(guān),可以繼續(xù)關(guān)注我們的行業(yè)資訊板塊。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI