内容显示页
 
类别:数据库+SQL | 浏览(652) | 2007-2-6 20:50:00
sql语句实现:根据B表的汇总结果更新A表里的数据,A.username=B.username

sql语句实现:根据B表的汇总结果更新A表里的数据,A.username=B.username

如:
TableA username是唯一的
id username income
1 user1 20
2 user2 20
3 user3 30
4 user4 40
5 user5 50

TableB
id username incomeMonth
1 user1 20
2 user1 20
3 user1 30
4 user3 40
5 user3 50
6 user3 40
7 user3 50

我想实现,
TableB根据username汇总的incomeMonth
更新到TableA里相应的income里,
两表有这样的关系
TableB.username=TableA.username

解决方法


update a set income = b.sumInMonth
from TableA a
join (select username ,sum(incomeMonth) as sumInMonth from TableB group by username ) b on a.username = b.username

update TableA
set income = b.incomemonth
from tableA a,(select username , sum(incomemonth) as incomemonth from tableb group by username) b
where a.username = b.username


引用本页地址:http://www.yongfa365.com/item/8368a7bc80285681.html
 
 
相关链接
 
网友评论:
姓名: 记住我
网址:
邮箱:
内容:
验证码:  验证码图片 看不清? 换张图试试
 
     
 
 
文章分类
 
 
.Net + C#(73)
 
 
ASP+VBS(161)
 
 
 
Linux(10)
 
 
 
web 2.0(26)
 
 
 
 
 
心程(68)
 
生活(97)
 
 
     

Power by :柳永法(yongfa365)'Blog  | 京ICP备07011491号  QQ:64049027  E-mail:64049027@qq.com yongfa365'CodePlex yongfa365'CodeGoogle

申请友情链接 要求:跟本站主题相类似正规网站,双方交换为首页位置

转载请注明来源,以便后人及时得到最新、修正、加强版!!!