select c.customerid,c.firstname,c.lastname,s.customerid
from sales s,customers c
where s.customerid=*c.customerid and s.customerid is null
select c.customerid,c.firstname,c.lastname,s.customerid
from sales s right join customers c
on c.customerid=s.customerid 
where s.customerid is null
这两个应该都是右连接吧?但为什么执行出来不一样啊
题目是“查询没有购买过产品的客户”就是 customer 有的id ,sales 表中没有
第2种能出来正确答案
第一种为什么不行?结果是把s.customerid 全部付成为null拉
两种情况在“s.customerid is null” 是一样的,都是右连接,为什么加上条件就不一样啦?
帮忙给解答以下,谢谢

解决方案 »

  1.   

    use Test
    go
    --测个数据给楼主看看就明白了
    declare @t table(ID int)
    insert @t select 1
    insert @t select 2
    insert @t select 3
    declare @t2 table(ID int)
    insert @t2 select 3
    insert @t2 select 4
    insert @t2 select 5select 

    from 
    @t t 
    right join 
    @t2  t2 on t.ID=t2.id
    and 
    t.ID is null
    select 
    *
    from 
    @t t ,@t2 t2
    where 
    t.ID=*t2.ID and t.ID is nullD          ID          
    ----------- ----------- 
    NULL        3
    NULL        4
    NULL        5(所影响的行数为 3 行)ID          ID          
    ----------- ----------- 
    NULL        3
    NULL        4
    NULL        5(所影响的行数为 3 行)
      

  2.   

    在早期的 Microsoft® SQL Server™ 2000 版本中,使用 *= 和 =* 在 WHERE 子句中指定左、右外部联接条件。有时,该语法会导致有多种解释的不明确查询。******
    FROM 子句中指定遵从 SQL-92 的外部联接,不会导致上述不确定性。因为 SQL-92 语法更为精确,所以,本版中未包括有关在 WHERE 子句中使用旧的 Transact-SQL 外部联接语法的详细信息。以后的 SQL Server 版本可能不再支持该语法。任何使用 Transact-SQL 外部联接的语句都应改为使用 SQL-92 语法。SQL-92 标准支持 FROM 或 WHERE 子句中的内部联接规范。WHERE 子句中指定的内部联接不会出现与 Transact-SQL 外部联接语法相同的不确定性问题。
      

  3.   

    在兼容性级别 90(SQL2005)以取消这种连接方式
      

  4.   

    select   c.customerid,c.firstname,c.lastname,s.customerid 
    from   sales   s   right   join   customers   c 
    on   c.customerid=s.customerid   
    where   s.customerid   is   null 
    将 s.customerid   is   null 放到上面就一样了
      

  5.   

    如果customers表的数据其customerid比sales的customerid多的话(在customerid不为null的情况下),那结果不一样.
    否则两查询结果一样.另:应该是s.customerid is not null 吧?