A表字段更新為B表的字段值
T1表 結構
id name school
1 ming1 清華大學
2 ming2 北京大學
3 ming3 復旦大學
T2表 結構
id student school
11 ming1 牛津大學
12 ming3 劍橋大學
T1表的name 和 T2表的 student 值是 1對1 的對應。
需求:這兩個字段的值相同時,根據B表的school更新A表的school。
語句:
update t1 set school = (select school from t2 where name=student) where exists (select * from t2 as b where name = b.student);
結果:
T1表 結構
id name school
1 ming1 牛津大學
2 ming2 北京大學
3 ming3 劍橋大學
方法二:
update a set(a.province,a.city)=(select province,city from b where b.mobile=a.mobile)
注:用a.city=null不行的