当前位置: 代码迷 >> Sql Server >> 请问一个多表查询插入语句
  详细解决方案

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

热度:101   发布时间:2016-04-27 14:49:42.0
请教一个多表查询插入语句
A表是客户信息表(用户名不重复),字段: userName、UserLl
B表是业务表(各用户都有很多业务),字段:UserName 、YwMoney

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

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


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

------解决方案--------------------
SQL code
insert c(UserName,UserLl,YwMoney)select a.userName,a.UserLl,isnull(b.YwMoney,0)from aleft join (select UserName,sum(YwMoney) as YwMoney from b group by UserName) bon a.UserName=b.UserName
------解决方案--------------------
探讨
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.Use……

------解决方案--------------------
SQL code
select t1.userName,t1.UserLl,YwMoney=sum(YwMoney) into C from A t1 inner join B t2 on t1.userNmae=t2.userName group by t1.userName,t1.UserLl
------解决方案--------------------
SQL code
insert into    cselect   *from   (    select  userName,UserLl,0 as YwMoneyfrom a    union all    select UserName,'',sum(YwMoney) as YwMoney from b group by UserName)t
------解决方案--------------------
探讨
SQL code

insert into
c
select
*
from
(
select userName,UserLl,0 as YwMoneyfrom a
union all
select UserName,'',sum(YwMoney) as YwMoney from b group by UserName)t
  相关解决方案