首页 > 其他 > 详细

A表字段更新为B表的字段值

时间:2016-07-20 11:46:36      阅读:169      评论:0      收藏:0      [点我收藏+]

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不行的

A表字段更新为B表的字段值

原文:http://www.cnblogs.com/zzzzbk/p/5687700.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!