1、select * from customer where 
exists(select * from orderswhere orders.客户编号=customer.客户编号)

select * from customer where exists
(select * from orders,customer where orders.客户编号=customer.客户编号)
有什末区别?
2、select a.* from orders a join orders b 
on a.客户编号=b.客户编号 and a.订单号<>b.订单号
帮忙解释一下这条语句的意思。

解决方案 »

  1.   

    create table orders(客户编号 int,订单号 int)
    insert into orders values(1,1)
    insert into orders values(1,2)
    insert into orders values(1,3)
    insert into orders values(2,1)
    insert into orders values(2,2)
    create table customer(客户编号 int,订单号 int)
    insert into customer values(1,1)
    insert into customer values(2,1)
    go
    select * from customer where exists (select * from orders where orders.客户编号 = customer.客户编号) 
    /*
    客户编号        订单号         
    ----------- ----------- 
    1           1
    2           1
    */
    select * from customer where exists (select * from orders , customer where orders.客户编号 = customer.客户编号) 
    /*
    客户编号        订单号         
    ----------- ----------- 
    1           1
    2           1
    */
    select a.* from orders a join orders b on a.客户编号 = b.客户编号 and a.订单号 <> b.订单号 order by a.客户编号,a.订单号
    /*
    客户编号        订单号         
    ----------- ----------- 
    1           1
    1           1
    1           2
    1           2
    1           3
    1           3
    2           1
    2           2
    */drop table orders,customer
      

  2.   


    1、select   *   from   customer   where   exists 
    (select   *   from   orders  where   orders.客户编号=customer.客户编号) 
    --取customer表中客户编号存在于orders表中的数据。
    select   *   from   customer   where   exists 
    (select   *   from   orders,customer   where   orders.客户编号=customer.客户编号) 
    --与上句没有区别。。
      

  3.   

    1.  2个应该都等同与  
    select   a.*   
    from   orders a inner join customer b  
    where   a.客户编号=b.客户编号2.  应该只是比1多了一个条件 a.订单号 <> b.订单号
      

  4.   

    我感觉一下两句都能查询有订单的客户信息,不过运行的结果是1可以而2的结果不对。就像我在3楼贴出来的结果,2运行的结果多出了没有订单的客户信息,请问为什末?
    1、select   *   from   customer   where   
    exists(select   *   from   orders where   orders.客户编号=customer.客户编号) 
    2、select   *   from   customer   where   exists 
    (select   *   from   orders,customer   where   orders.客户编号=customer.客户编号) 
      

  5.   

    1、select   *   from   customer   where   
    exists(select   *   from   orderswhere   orders.客户编号=customer.客户编号) 
    对curtomer中的每条记录,查询 select   *   from   orders  中客户编号=curtomer表中当前记录的客户编号,如果查询有结果,则返回customer表的记录
    如果不存在,则不返回记录.
    其中子查询与外面表customer有关连select   *   from   customer   where   exists 
    (select   *   from   orders,customer   where   orders.客户编号=customer.客户编号) 
    对curtomer中的每条记录, 检验查询select   *   from   orders,customer   where   orders.客户编号=customer.客户编号是否存在,
    如果存在,则返回记录,如果不存在则不返回,其中select   *   from   orders,customer   where   orders.客户编号=customer.客户编号
    是一独立的查询,并未与外面的customer表发生关连.本人描述能力一般,可能说得不是很清楚.
    2、select   a.*   from   orders   a   join   orders   b   
    on   a.客户编号=b.客户编号   and   a.订单号 <> b.订单号 
    帮忙解释一下这条语句的意思。
    orders表的记录取出取别名 为a,再取出orders表的记录取别名为 B
    a与b 内连接,取出其中 两记录集中客户编号相同且订单号不同的记录
      

  6.   

    我感觉一下两句都能查询有订单的客户信息,不过运行的结果是1可以而2的结果不对。就像我在3楼贴出来的结果,2运行的结果多出了没有订单的客户信息,请问为什末?
    因为1.中第二个查询中的子查询是一独立的查询(说它独立,是因为将子查询单独拿出,可独立运行),并不与外部的customer表发生关连,如果查询存在,则customer表中的记录全部出来,
    如果查询不存在,则customer表中的记录全部不出来.
       而第一个查询中,每扫描过一条记录,都将此记录的 客户编号取出作为与子查询的客户编号比较的where条件,根据子查询是否存在,决定记录是否返回.
      

  7.   

    cxmcxm————————
    正解!
      

  8.   

    谢谢cxmcxm 的回答。第一个问题我已经弄明白了。
    第二个我问得有点问题。有空的话再帮我看看说说第二个问题吧。先谢了哈!以下是我orders表中的数据:
    订单号         货品名称                 客户编号        数量          总金额                   订货日期                                                   
    ----------- -------------------- ----------- ----------- --------------------- ------------------------------------------------------ 
    1           pen                  2           10          156.0000              2003-03-24 00:00:00.000
    2           pen                  4           70          1092.0000             2003-03-24 00:00:00.000
    3           desk                 2           10          2000.0000             2003-03-24 00:00:00.000
    4           desk                 1           20          4000.0000             2003-03-24 00:00:00.000
    5           book                 2           5           NULL                  2003-03-25 00:00:00.000
    当我执行
    select   a.*   from   orders   a   join   orders   b   
    on   a.客户编号=b.客户编号   and   a.订单号 <> b.订单号 
    是出来的结果是:
    订单号         货品名称                 客户编号        数量          总金额                   订货日期                                                   
    ----------- -------------------- ----------- ----------- --------------------- ------------------------------------------------------ 
    3           desk                 2           10          2000.0000             2003-03-24 00:00:00.000
    5           book                 2           5           NULL                  2003-03-25 00:00:00.000
    1           pen                  2           10          156.0000              2003-03-24 00:00:00.000
    5           book                 2           5           NULL                  2003-03-25 00:00:00.000
    1           pen                  2           10          156.0000              2003-03-24 00:00:00.000
    3           desk                 2           10          2000.0000             2003-03-24 00:00:00.000
    我想问一下为什末结果显示出来了两次啊?
      

  9.   

    我想问一下为什末结果显示出来了两次啊?因爲 a表裏和b表裏滿足條件的分別是
    a:订单号 1 3 5
    b:订单号 1 3 5
    a的订单号 1 不等于 b的订单号 3 5
    a的订单号 3 不等于 b的订单号 1 5
    a的订单号 5 不等于 b的订单号 1 3
    所以每個就出兩條阿 不想出重復紀錄的話 就用distinct
      

  10.   

    我想问一下为什末结果显示出来了两次啊?因爲 a表裏和b表裏滿足條件的分別是
    a:订单号 1 3 5
    b:订单号 1 3 5
    a的订单号 1 不等于 b的订单号 3 5
    a的订单号 3 不等于 b的订单号 1 5
    a的订单号 5 不等于 b的订单号 1 3
    所以每個就出兩條阿 不想出重復紀錄的話 就用distinct
      

  11.   

    我想问一下为什末结果显示出来了两次啊?因爲 a表裏和b表裏滿足條件的分別是
    a:订单号 1 3 5
    b:订单号 1 3 5
    a的订单号 1 不等于 b的订单号 3 5
    a的订单号 3 不等于 b的订单号 1 5
    a的订单号 5 不等于 b的订单号 1 3
    所以每個就出兩條阿 不想出重復紀錄的話 就用distinct