mysql的update join

有两个表
表1 a 字段 id name
表2 b 字段 id name
现在我想把表2的值更新到表1 ,当他们的id一样的时候

我的第一个想法是要用存储过程,但是感觉这么简单没必要用存储过程吧.
尝试搜了下update join 还真有.
语法:
UPDATE table_1 a LEFT JOIN table_2 b ON b.id = a.id SET a.name=b.name

下面是我时间项目中的sql:

UPDATE T_daily t1 
  join (select sum(ctuv) as ctuv,sum(notctuv) as notctuv,dt,pid,spid from 
(SELECT 
case when uvType='电信手机用户' then uv ELSE 0 end as  ctuv,
case when uvType<>'电信手机用户' then uv ELSE 0 end as notctuv,dt,pid,spid
from T_uv_d where dt>='2013-08-01' )tmp
group by dt,pid,spid) t2
 on t1.dt=t2.dt and t1.pid=t2.pid and t1.spid=t2.spid
 set  t1.ctuv=t2.ctuv,t1.notctuv=t2.notctuv 

猜你喜欢

转载自huangyunbin.iteye.com/blog/1939951