有两个表:
产品表Product(ID,ProductDate,ProductName,ProductSerial,Amount)分别代表 主键,生产日期,产品名称,生产批次,生产量
销售表Sale(ID,SaleDate,ProductName,ProductSerial,PurchaseAgent,Amount)分别代表 主键,卖出日期,产品名称,生产批次,进货商名称,进货量)现在我想获得:指定的进货商在指定日期,指定产品的一天的所有批次上的进货量declare @TD smalldatetime,@Agent nvarchar(50),@PN nvarchar(50)
set @TD='2010/4/27'--日期
set @Agent='aa'--进货商名称
set @PN='bb'--产品名称
select p.ProductDate,p.ProductName,p.ProductSerial,s.PurchaseAgent,s.Amount from Product p 
    left outer join Sale s on (p.ProductDate=s.SaleDate and p.ProductName=s.ProductName and p.ProductSerial=s.ProductSerial)  
    where p.ProductDate=@TD and s.PurchaseAgent=@Agent and p.ProductName=@PN执行查询之后为什么返回的结果中只有进货商进了的批次才显示出来,没有进货的批次(但实际有生产的批次)就没有记录??

解决方案 »

  1.   

    那你要用FULL JOINdeclare @TD smalldatetime,@Agent nvarchar(50),@PN nvarchar(50)
    set @TD='2010/4/27'--日期
    set @Agent='aa'--进货商名称
    set @PN='bb'--产品名称
    select isnull(p.ProductDate,s.ProductDate) ProductDate,--另外几个字段类似
    p.ProductName,p.ProductSerial,s.PurchaseAgent,s.Amount from Product p  
      full join Sale s on (p.ProductDate=s.SaleDate and p.ProductName=s.ProductName and p.ProductSerial=s.ProductSerial)   
      where p.ProductDate=@TD and s.PurchaseAgent=@Agent and p.ProductName=@PN
      

  2.   

    declare @TD smalldatetime,@Agent nvarchar(50),@PN nvarchar(50)
    set @TD='2010/4/27'--日期
    set @Agent='aa'--进货商名称
    set @PN='bb'--产品名称
    select p.ProductDate,p.ProductName,p.ProductSerial,s.PurchaseAgent,s.Amount from Product p  
      left outer join Sale s on (p.ProductDate=s.SaleDate and p.ProductName=s.ProductName and p.ProductSerial=s.ProductSerial)   
    and s.PurchaseAgent=@Agent  where p.ProductDate=@TD  and p.ProductName=@PN
      

  3.   

    declare @TD smalldatetime,@Agent nvarchar(50),@PN nvarchar(50)
    set @TD='2010/4/27'--日期
    set @Agent='aa'--进货商名称
    set @PN='bb'--产品名称
    select isnull(p.ProductDate,s.ProductDate) ProductDate,
    isnull(p.ProductName,s.ProductName) ProductName,
    isnull(p.ProductSerial,s.ProductSerial) ProductSerial,
    isnull(s.PurchaseAgent,0) PurchaseAgent,
    isnull(s.Amount,0) Amount 
    from Product p  
      full join Sale s 
    on (p.ProductDate=s.SaleDate and p.ProductName=s.ProductName and p.ProductSerial=s.ProductSerial)   
    where p.ProductDate=@TD and s.PurchaseAgent=@Agent and p.ProductName=@PN你这样试下
      

  4.   

    declare @TD smalldatetime,@Agent nvarchar(50),@PN nvarchar(50)
    set @TD='2010/4/27'--日期
    set @Agent='aa'--进货商名称
    set @PN='bb'--产品名称
    select isnull(p.ProductDate,s.ProductDate) ProductDate,
        isnull(p.ProductName,s.ProductName) ProductName,
        isnull(p.ProductSerial,s.ProductSerial) ProductSerial,
        isnull(s.PurchaseAgent,0) PurchaseAgent,
        isnull(s.Amount,0) Amount 
    from Product p  
      full join Sale s 
        on (p.ProductDate=s.SaleDate and p.ProductName=s.ProductName and p.ProductSerial=s.ProductSerial)   
      and p.ProductDate=@TD and s.PurchaseAgent=@Agent and p.ProductName=@PN --嗯,这个要用and
      

  5.   

    josy:谢谢,我试了你的语句,能够返回正确的结果,但是为什么要把进货商的限制条件放在on 之中呢?htl258:谢谢!我也试了你的语句,但都有点儿问题。