mysql update 多表實例講解
我們先來看如下實例:
實例一:
update tab1 set tab1.商品尺寸 = (select tab2.商品尺寸 from tab2 where tab2.商品顏色 = tab1.商品顏色) where tabl1.商品顏色 in (select tab2.商品顏色 from tab2)
繼續如下實例:
UPDATE CMS_Document A,CMS_Template B SET A.RuleType2=B.RuleType WHERE A.TemplateID = B.TemplateID
一個替代的方法可以采用:
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),'未知')
MySQL多表更新實例三:
update contact c, contactdroit cd set c.user_name = '$username', c.nom = '$lastname', c.prenom = '$firstname', c.passcode = '$password', cd.droit_id = '$droitid' where c.contact_id = '$id' and c.contact_id = cd.contact_id;
mysql關聯多表進行update更新操作
UPDATE Track INNER JOIN MV ON Track.trkid=MV.mvid SET Track.is_show=MV.is_show WHERE trkid<6
等同於
UPDATE Track,MV SET Track.is_show=MV.is_show WHERE Track.trkid=MV.mvid and trkid<6
實例四:
UPDATE product p INNER JOIN productPrice pp ON p.productId = pp.productId SET pp.price = pp.price * 0.8,p.dateUpdate = CURDATE() WHERE p.dateCreated < '2004-01-01'
注:更多精彩文章請關注三聯編程教程欄目。