本人想做一个非常简单的仓库管理
A表是入库表
B表是出库表
入库时间和出库时间都想做成datetime,只是不太会现在A:
入库时间--2010-8-28  2部 手机
         2010-8-29   3台 电脑
         2010-8-30   4个 MP4
现在B:
出库时间--2010-9-2   1部 手机
          2010-9-29  1台 电脑想得到的结果:手机    1部
             电脑    2台
             MP4     4个  
--建表--
if object_id('[A]') is not null drop table [A]
go
create table A
(rq varchar(20),inname varchar(20) ,innum int)
insert A
select '20100828','shouji',2 union all
select '20100829','diannao',3if object_id('[B]') is not null drop table [B]
go
create table B
(rq varchar(20),outname varchar(20) ,outnum int)
insert B
select '20100902','shouji',1 union all
select '20100929','diannao',2--查询--
select inname,(A.innum - B.outnum) as num from A union all B where (A.inname=B.outname)
我写的意思是:如果入库没有那4个MP4
请问我错在哪里?
要是要得到那4个MP4,又该如何写
谢谢各位了

解决方案 »

  1.   

    select inname,(A.innum - B.outnum) as num from A left join B on (A.inname=B.outname)
      

  2.   

    那如果入库的时候有4个MP4
    而这4个MP4一直没有出库
    那应该怎么写呢?
      

  3.   

    select A.inname,(A.innum-B.outnum) num from A,B where A.inname=B.outname
    union all
    select A.inname,A.innum num from A where A.inname not in(select B.outname from B)
    go
      

  4.   


    select inname,(A.innum - isnull(B.outnum,0)) as num 
    from A left join B on (A.inname=B.outname)