日期:2014-05-16  浏览次数:21003 次

求个sql,在mysql下运行
表1
id count_2
1 0
2 0

表2
id id1
1 1
2 1
3 2
4 2
5 2


求个更新语句

使得表1变成
1 2
2 3
也就是说

表2 的id1关联表1的id
表1中的count_2 就是 表2中的count后的值

------解决方案--------------------
update tb1 LEFT JOIN (select id1,count(*) as num
from tb2
group id1
)tb3 ON tb1.id = tb3.id1
set tb1.count_2=IFNULL(tb3.num,0)