update帶子查詢更新

現在要做一下數據移植,需要更新相關的數據,需要在mysql中更新時不能將更新的表作爲查詢的表。
總結一下:
一:單表更新時
例如: update customer set category = 1 WHERE deleteflag = 0 and name = '22';
注意不要子查詢,mysql是不允許的。
二:帶子查詢的複雜更新
如:
update tb a,
(select time,name
from tt )b
set time4=b.col
where a.name=b.name and a.time1=b.time;

注意點:
1、update 時,更新的表不能在set和where中用於子查詢;
2、update 時,可以對多個表進行更新(sqlserver不行);
如:update ta a,tb b set a.Bid=b.id ,b.Aid=a.id;
3、update 後面可以做任意的查詢,這個作用等同於from;

參考的文章:感謝原創,弄了半天才弄出來,看到這裏,豁然開朗。
http://blog.csdn.net/xys_777/article/details/5793565

原需求:需要將info_stu_train_info的grade字段設成info_person_info中學號的前四位,兩表根據personId關聯,語句如下:
update info_stu_train_info t,(select left(perNum,4) as grade,personId from info_person_info where perNum is not null and perNum like '20%') as i set t.grade=i.grade
where t.personId=i.personId;
發佈了57 篇原創文章 · 獲贊 6 · 訪問量 7968
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章