语句A:
select Sto_Code,IsNull(STA_ID,''),A.Mat_Code,B.Mat_Code,BAL_Endqty
  from Tmp1 A 
  Left Join Tmp2 B On A.Mat_Code=B.Mat_Code
 where BAL_year='2006' and BAL_Month='05'  
order by B.Mat_Code语句B:
select Sto_Code,IsNull(STA_ID,''),A.Mat_Code,B.Mat_Code,BAL_Endqty
  from Tmp1 A ,Tmp2 B
 where A.Mat_Code=B.Mat_Code and BAL_year='2006' and BAL_Month='05'  
order by B.Mat_Code
这两条语句执行的结果是不一样,下在结果的其中一行:
Sto_Code        A.Mat_Code     B.Mat_Code  BAL_EndAmt
C001            27052020200     NULL        142.0000
为什么语句A执行出来有一上面的结果呢?B.Mat_Code是NULL的值,而语句B执行出来没有上面这行,
在Tmp2表出确实找不到Mat_Code等于27052020200的记录。

解决方案 »

  1.   

    你自己看看联机就可以了此外,你把B语句写成select Sto_Code,IsNull(STA_ID,''),A.Mat_Code,B.Mat_Code,BAL_Endqty
      from Tmp1 A ,Tmp2 B
     where A.Mat_Code*=B.Mat_Code and BAL_year='2006' and BAL_Month='05'  
    order by B.Mat_Code再看看结果
      

  2.   

    明白了左联接。现在有个问题是:
    select Sto_Code,IsNull(STA_ID,''),A.Mat_Code,BAL_Endqty
      from Tmp1 A  where Exists(Select Mat_Code From Tmp2 Where Mat_Code=A.Tmp1)  
    上面的语句为什么得到的结果中有包括Mat_Code等于27052020200的记录呢?
      

  3.   

    上面的语句错了,是下面的
    select Sto_Code,IsNull(STA_ID,''),A.Mat_Code,BAL_Endqty
      from Tmp1 A  where Exists(Select Mat_Code From Tmp2 Where Mat_Code=A.Mat_Code)  
    上面的语句为什么得到的结果中有包括Mat_Code等于27052020200的记录呢?
      

  4.   

    因为Tmp1表与Tmp2 表在字段Mat_Code中均存在27052020200这条记录啊!
      

  5.   

    语句A是左联接.在不考虑WHERE的情况下,返回左侧表的所有行.
    语句B是内连接.在不考虑WHERE的情况下,返回左侧表中与右侧表在Mat_Code列值上相同的行,即二表在Mat_Code列上共有的行.
    楼主上面的语句相当于内连接,之所以会出现
    Mat_Code等于27052020200
    的记录,可能是楼主忘了加上:
    BAL_year='2006' and BAL_Month='05'  
    这个条件.请楼主加上这个条件再试试.
      

  6.   

    ....left outer join 和 inner join 的区别