sql,三表连接查询,跪求
三个表
t1商品表
id size num date
1 l 12 2007-9-1
2 m 12 2007-9-1
t2 商品出货明晰表
id shangpin num date
1 1 12 2007-9-19
2 1 12 2007-9-20
3 1 2 2007-9-20
t3 商品入库明晰表
id shangpin num date
1 1 20 2007-9-20
2 1 20 2007-9-20
如何通过sql语句查询到
2007-9-20这一天1商品出货,和入库多少
也就是得出以下结果
id 商品 出货 入库 时间
1 1 14(12+2) 40(20+20) 2007-9-20
2 2 0 0 2007-9-20
谢谢,跪求sql语句
------解决方案--------------------create table t1(id int, size varchar(10), num int, date datetime)
insert into t1 values(1, 'l ', 12, '2007-9-1 ')
insert into t1 values(2, 'm ', 12, '2007-9-1 ')
create table t2(id int, shangpin int, num int, date datetime)
insert into t2 values(1, 1, 12, '2007-9-19 ')
insert into t2 values(2, 1, 12, '2007-9-20 ')
insert into t2 values(3, 1, 2, '2007-9-20 ')
create table t3(id int, shangpin int, num int, date datetime)
insert into t3 values(1, 1, 20, '2007-9-20 ')
insert into t3 values(2, 1, 20, '2007-9-20 ')
go
select t1.id,isnull(m.num,0) 出货,isnull(n.num,0) 入库 , 时间 = '2007-09-20 ' from t1
left join (select shangpin,sum(num) num from t2 where date = '2007-09-20 ' group by shangpin) m on t1.id = m.shangpin
left join (select shangpin,sum(num) num from t3 where date = '2007-09-20 ' group by shangpin) n on t1.id = n.shangpin
drop table t1,t2,t3
/*
id 出货 入库 时间
----------- ----------- ----------- ----------
1 14 40 2007-09-20
2 0 0 2007-09-20
(所影响的行数为 2 行)
*/
------解决方案----------------------借用楼上数据
create table t1(id int, size varchar(10), num int, date datetime)
insert into t1 values(1, 'l ', 12, '2007-9-1 ')
insert into t1 values(2,