table1表:编号 价格  
12    100
13    200
15    150
18    220table2表:编号  日期       数量
12    2006-9-1    2
15    2006-8-1    1要求:在table2表 日期 等于 2006-9-1时结果编号 价格   数量 
12    100    2
13    200    0
15    150    0
18    220    0

解决方案 »

  1.   

    select a.编号,a.价格,isnull(b.数量,0) from table1 a
    left join table2 b on a.编号= b.编号 
    where datediff(dd,b.日期,'2006-9-1') = 0
      

  2.   

    select a.编号,a.价格,isnull(b.数量,0) from table1 a
    left join table2 b on a.编号= b.编号 
    and datediff(dd,b.日期,'2006-9-1') = 0
      

  3.   

    楼上朋友正确,我的回复中
    where datediff(dd,b.日期,'2006-9-1') = 0
    应为
    and datediff(dd,b.日期,'2006-9-1') = 0
      

  4.   

    楼上的方法
    如果table2表 日期 要等于 2006-7-1的时候,
    但table2中没有这个日期,怎么一条也不出现了
      

  5.   

    如果在table2中没有找到日期时应该结果是数据都为0编号 价格   数量 
    12    100    0
    13    200    0
    15    150    0
    18    220    0
      

  6.   

    declare @t1 table(编号 int,价格 int)
    declare @t2 table(编号 int,日期 datetime,数量 int)
    insert @t1
    select 12,    100 union all
    select 13,    200 union all
    select 15,    150 union all
    select 18,    220
    insert @t2
    select 12,    '2006-9-1',    2 union all
    select 15,    '2006-8-1',    1select a.编号,a.价格,isnull(b.数量,0) as 数量 from @t1 a
    left join @t2 b on a.编号= b.编号 
    and datediff(dd,b.日期,'2006-7-1') = 0/*结果
    编号    价格    数量
    12      100     0
    13      200     0
    15      150     0
    18      220     0
    */
      

  7.   

    hellowork(一两清风):你的方法可以了我还有一部分程序在Microsoft Access的Mdb数据库
    中这个方法就不能用
    请问怎么修改?
      

  8.   

    呵呵,ACCESS我可不会.好像是ACCESS中对表别名的支持不如SQLSERVER.