日期:2014-05-18  浏览次数:20451 次

求sql多表连接查询一条,谢谢
a表(服装)
id(vchar)   size(vchar)   number(库存)   date(时间)
1                         L                           23                         2007-02-02
2                         X                           34                         2007-02-02
3                         M                           45                         2007-02-02  
b表(出货记录)
id(vchar)   aid(a表id)   no(出库量)date(时间)
1                         2                           10                         2007-09-20
2                         2                           05                         2007-09-20
现在需要连接a表和b表得到以下查询结果
统计当月同种物品出货数据
怎么写sql语句啊,我想打印出
c表
id       size     本月出货
1         L             0
2         X             15  
3         M             0

------解决方案--------------------
--原始数据:@a
declare @a table(id int,size varchar(1),number int,date datetime)
insert @a
select 1, 'L ',23, '2007-02-02 ' union all
select 2, 'X ',34, '2007-02-02 ' union all
select 3, 'M ',45, '2007-02-02 '
--原始数据:@b
declare @b table(id int,aid int,no int,date datetime)
insert @b
select 1,2,10, '2007-09-20 ' union all
select 2,2,05, '2007-09-20 '

select a.id,a.size,本月出货=sum(isnull(b.no,0)) from @A a left join @B b on a.id=b.aid group by a.id,a.size order by a.id

/*
id size 本月出货
1 L 0
2 X 15
3 M 0
*/