+ -
当前位置:首页 → 问答吧 → 请教一个多表查询插入语句

请教一个多表查询插入语句

时间:2011-11-25

来源:互联网

A表是客户信息表(用户名不重复),字段: userName、UserLl
B表是业务表(各用户都有很多业务),字段:UserName 、YwMoney

现在想将A表和B表综合插入到C表中

C表字段: UserName UserLl YwMoney(要合计)


请问这个SQL要怎么写呢?请高手指点小弟。

作者: yangqi613   发布时间: 2011-11-25

SQL code
insert c(UserName,UserLl,YwMoney)
select a.userName,a.UserLl,isnull(b.YwMoney,0)
from a
left join (select UserName,sum(YwMoney) as YwMoney from b group by UserName) b
on a.UserName=b.UserName

作者: FlySQL   发布时间: 2011-11-25