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

来源:互联网 发布:程控裁纸机 编程 编辑:程序博客网 时间:2024/04/29 12:33

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

-----
个人实例:
update fes_rtu f set dev_id = (select o.id from obj_dev o where o.tag_name like  f.correlative_id||'') where  f.correlative_id > 0 ;
1 0
原创粉丝点击