联合查询的方法
现有以下两个表:
表A:
product machine model mdate aqty
pa 5 1 2013-6-3 1000
pa 6 1 2013-6-4 3000
pa 6 1 2013-6-3 1500
表B:
product machine model mdate bqty
pa 6 1 2013-6-3 5000
pa 6 1 2013-6-4 4000
要得到表C:
product machine model mdate cqty1 cqty2
pa 5 1 2013-6-3 1000 0
pa 6 1 2013-6-3 1500 5000
pa 6 1 2013-6-4 3000 4000
--------------------
请问各位如何编写SQL句子?谢谢。
-------------------
------解决方案--------------------select 'pa' as product,5 as machine,1 as model,'2013-6-3' as mdate,1000 as aqty into #A
union select 'pa' as product,6 as machine,1 as model,'2013-6-4' as mdate,3000 as aqty
union select 'pa' as product,6 as machine,1 as model,'2013-6-3' as mdate,1500 as aqty
select 'pa' as product,6 as machine,1 as model,'2013-6-3' as mdate,5000 as bqty into #B
union select 'pa' as product,6 as machine,1 as model,'2013-6-4' as mdate,4000 as bqty
select * from #A
select * from #B
select a.product,a.machine,a.model,a.mdate,a.aqty,b.bqty from #A a
left join #B b on a.product=b.product and a.machine=b.machine and a.model=b.model and a.mdate=b.mdate
union
select a.product,a.machine,a.model,a.mdate,b.aqty,a.bqty from #B a
left join #A b on a.product=b.product and a.machine=b.machine and a.model=b.model and a.mdat