=*
*=
這是SQL7.0是方法在oracle可這樣用,MS SQL最好不要用,會造成數據不準確現象;聯機上有說明,曾測過語句多表連接時會出現 

解决方案 »

  1.   

    改為 left join 左聯
    right join 右聯
      

  2.   


    create table t1(id int, feild int)
    insert into t1 values(1 , 1)
    insert into t1 values(1 , 2)
    insert into t1 values(1 , 3)
    insert into t1 values(1 , 4)
    insert into t1 values(2 , 1)
    insert into t1 values(2 , 2)
    create table t2(id int, feild int)
    insert into t2 values(1 , 1)
    insert into t2 values(1 , 2)
    insert into t2 values(1 , 5)
    insert into t2 values(1 , 6)
    insert into t2 values(2 , 1)
    insert into t2 values(2 , 3)
    goselect t1.*,t2.* from t1 left join t2 on t1.id=t2.id  and t1.feild=1
    /*
    id          feild       id          feild       
    ----------- ----------- ----------- ----------- 
    1           1           1           1
    1           1           1           2
    1           1           1           5
    1           1           1           6
    1           2           NULL        NULL
    1           3           NULL        NULL
    1           4           NULL        NULL
    2           1           2           1
    2           1           2           3
    2           2           NULL        NULL(所影响的行数为 10 行)
    */select t1.*,t2.* from t1 left join t2 on t1.id=t2.id  where t1.feild=1 
    /*
    id          feild       id          feild       
    ----------- ----------- ----------- ----------- 
    1           1           1           1
    1           1           1           2
    1           1           1           5
    1           1           1           6
    2           1           2           1
    2           1           2           3(所影响的行数为 6 行)
    */select t1.*,t2.* from t1 left join t2 on t1.id=t2.id  and t2.feild=1
    /*
    id          feild       id          feild       
    ----------- ----------- ----------- ----------- 
    1           1           1           1
    1           2           1           1
    1           3           1           1
    1           4           1           1
    2           1           2           1
    2           2           2           1(所影响的行数为 6 行)
    */select t1.*,t2.* from t1 left join t2 on t1.id=t2.id  where t2.feild=1 
    /*
    id          feild       id          feild       
    ----------- ----------- ----------- ----------- 
    1           1           1           1
    1           2           1           1
    1           3           1           1
    1           4           1           1
    2           1           2           1
    2           2           2           1(所影响的行数为 6 行)
    */drop table t1 , t2*= 的写法已经不常用了,建议楼主改成 left join 
    select t1.*,t2.* from t1 left join t2 on t1.id=t2.id  and t1.feild=1
    此句中的and 是作为一个连接条件存在的,即:只有t1.feild= 1 的数据才与 t2左连接 t1.feild 不等与1的数据不进行连接,所以他们对应的t2的结果全部为null
    select t1.*,t2.* from t1 left join t2 on t1.id=t2.id  where t1.feild=1 
    此句中的and是做为对连接后的结果进行筛选操作的,t1.feild <> 1的数据根本就不在结果集中显示!
    至于t2.feild 条件放在where的前后结果都一样,如果把此处的left join 换成right join 结果很明显就不一样。
    总的来说在使用left join 时and条件放在where之前的话是将此条件作为一个衡量第二表连接时的条件的一部分.
    放在where 之后的话是对连接之后的表进行筛选.
    另外如果是把 outer Join 换成 inner Join的话,条件放在什么地方都一样!
      

  3.   


    那就是SQL Server的BUG呢?
      

  4.   


    把源代码改成如下:
    select a.userid
    ,b.userid, 
    a.subname as dept
    ,a.deptname
    ,a.subname
    ,a.subname as groupname
    ,a.code,a.[name]
    ,a.pydate
    ,b.userid as checktime 
    from uvw_nzlemployee a 
    left join (select distinct userid from checkinout where checktime between '2009-01-31' and '2009-01-31 23:59:59') b  
    on a.userID = b.userID
    and a.userid=1617 
    and b.userid is null 
    and a.userid=1617 
    order by a.userid 现在如果把and b.userid is null 注销掉时应该数据量会增加的
      

  5.   

    已经不能用join了,因为视图里面已经存在JOIN 的.所以只能用这种方式.
      

  6.   

    呵呵,谢谢各位,想到办法了,可以先用不带AND 条件NULL的去掉,插入临时表.
    再用临时表过滤此条件.
    已经解决.