本来对oracle中的左右连接已经非常熟悉,但是看到项目中写的各式各样的连接自己也迷惑了(+﹏+)~。摘出来,希望火眼金睛o(≧v≦)o~~的朋友指点一二。先谢谢了O(∩_∩)O。为了容易理解,简化了实际复杂的sql,但结构类似问题1:  A文(项目使用的):    select  AA.x,BB.y,CC.z    from AA,BB,CC    where AA.id1 = BB.id1(+) and               AA.id2 = BB.id2(+) and               BB.id1 = CC.id1(+) and               BB.id2 = CC.id2(+) and               AA.m = 'test'  B文:    select  AA.x,BB.y,CC.z    from AA,BB,CC    where AA.id1 = BB.id1(+) and               AA.id2 = BB.id2(+) and               AA.id1 = CC.id1(+) and               AA.id2 = CC.id2(+) and               AA.m = 'test'sql本身想以AA表作为主表查询,但在A文中偷换了主表,B文似乎没什么悬念。想请教的是A文中移花接木的做法会产生问题么?问题2:    C文(项目使用的):       select  AA.x,BB.y,CC.z       from AA          left join BB on               AA.id1 = BB.id1 and               AA.id2 = BB.id2         left join CC on               BB.id1 = CC.id1 and               BB.id2 = CC.id2        where  AA.m = ‘test’     D文:         select  AA.x,BB.y,CC.z                         from AA          left join BB on               AA.id1 = BB.id1 and               AA.id2 = BB.id2         left join CC on               AA.id1 = CC.id1 and               AA.id2 = CC.id2        where  AA.m = ‘test’   也是AA表为主表,问题同上面的,C文的写法与D文相比会差生查询结果上的差异么?
如果觉得A文与C文是一样sql的不用写法,费眼回答一个就行。

解决方案 »

  1.   

    C:
    AA.id1 = BB.id1 andAA.id2 = BB.id2BB.id1 = CC.id1 andBB.id2 = CC.id2D:
    AA.id1 = BB.id1 andAA.id2 = BB.id2AA.id1 = CC.id1 andAA.id2 = CC.id2
    C和D的连接条件不一样。
    除非A,B,C三表对于id1,id2是一一关联,那么上述语句的结果才能一样。
      

  2.   

    AA,BB,CC三个表对id1,id2是一一关联的
      

  3.   

    如果为一一关联,两语句效果一样。那么同时问你,既然是一一关联,为什么用left join,直接内连接即可,不必
      

  4.   

    抱歉-_-!,关系写错了。
    BB表和CC表是一一关联。AA表是主表。
      

  5.   

    id1和id2如果不是一一对应,那么结果肯定是不一样的,你最好做个测试看下。
      

  6.   


    c 跟d的左连接跟cc表的主表不一样怎么可能结果一样.
    c:
    left join CC on  BB.id1 = CC.id1 and  BB.id2 = CC.id2d:left join CC on  AA.id1 = CC.id1 and  AA.id2 = CC.id2
      

  7.   

    试了下.这几个语句应该是等价的.应该跟是不是一一对应没关系.不是一一对应的时候相当于在原来的基础上做个类笛卡尔积.
    SQL> select * from aa,bb,cc
      2  where aa.id1=bb.id1(+) and bb.id1=cc.id1(+) and aa.m='test';
     
            ID1 X          M                  ID1 Y                  ID1 Z
    ----------- ---------- ---------- ----------- ---------- ----------- ----------
              2 b          test                 2 b                    2 b
              1 a          test                                          
              5 e          test                                          
              4 d          test                 4 d                      
              3 c          test                 3 c                      
     
    SQL> 
    SQL> select * from aa left join  bb on aa.id1=bb.id1 and aa.m='test'
      2  left join cc on bb.id1=cc.id1
      3  ;
     
            ID1 X          M                  ID1 Y                  ID1 Z
    ----------- ---------- ---------- ----------- ---------- ----------- ----------
              2 b          test                 2 b                    2 b
              5 e          test                                          
              1 a          test                                          
              4 d          test                 4 d                      
              3 c          test                 3 c                      
     
    SQL> insert into aa select * from aa;
     
    5 rows inserted
     
    SQL> insert into bb select * from bb
      2  ;
     
    5 rows inserted
     
    SQL> insert into cc select * from cc;
     
    2 rows inserted
     
    SQL> 
    SQL> select * from aa,bb,cc
      2  where aa.id1=bb.id1(+) and bb.id1=cc.id1(+) and aa.m='test';
     
            ID1 X          M                  ID1 Y                  ID1 Z
    ----------- ---------- ---------- ----------- ---------- ----------- ----------
              2 b          test                 2 b                    2 b
              2 b          test                 2 b                    2 b
              2 b          test                 2 b                    2 b
              2 b          test                 2 b                    2 b
              2 b          test                 2 b                    2 b
              2 b          test                 2 b                    2 b
              2 b          test                 2 b                    2 b
              2 b          test                 2 b                    2 b
              1 a          test                                          
              1 a          test                                          
              5 e          test                                          
              5 e          test                                          
              3 c          test                 3 c                      
              3 c          test                 3 c                      
              3 c          test                 3 c                      
              3 c          test                 3 c                      
              4 d          test                 4 d                      
              4 d          test                 4 d                      
              4 d          test                 4 d                      
              4 d          test                 4 d                      
     
    20 rows selected
     
    SQL> 
    SQL> select * from aa left join  bb on aa.id1=bb.id1 and aa.m='test'
      2  left join cc on bb.id1=cc.id1
      3  ;
     
            ID1 X          M                  ID1 Y                  ID1 Z
    ----------- ---------- ---------- ----------- ---------- ----------- ----------
              2 b          test                 2 b                    2 b
              2 b          test                 2 b                    2 b
              2 b          test                 2 b                    2 b
              2 b          test                 2 b                    2 b
              2 b          test                 2 b                    2 b
              2 b          test                 2 b                    2 b
              2 b          test                 2 b                    2 b
              2 b          test                 2 b                    2 b
              5 e          test                                          
              1 a          test                                          
              5 e          test                                          
              1 a          test                                          
              3 c          test                 3 c                      
              3 c          test                 3 c                      
              3 c          test                 3 c                      
              3 c          test                 3 c                      
              4 d          test                 4 d                      
              4 d          test                 4 d                      
              4 d          test                 4 d                      
              4 d          test                 4 d                      
     
    20 rows selected
     
    SQL> 
    SQL> select * from aa left join  bb on aa.id1=bb.id1 and aa.m='test'
      2  left join cc on aa.id1=cc.id1;
     
            ID1 X          M                  ID1 Y                  ID1 Z
    ----------- ---------- ---------- ----------- ---------- ----------- ----------
              2 b          test                 2 b                    2 b
              2 b          test                 2 b                    2 b
              2 b          test                 2 b                    2 b
              2 b          test                 2 b                    2 b
              2 b          test                 2 b                    2 b
              2 b          test                 2 b                    2 b
              2 b          test                 2 b                    2 b
              2 b          test                 2 b                    2 b
              5 e          test                                          
              5 e          test                                          
              3 c          test                 3 c                      
              3 c          test                 3 c                      
              3 c          test                 3 c                      
              3 c          test                 3 c                      
              4 d          test                 4 d                      
              4 d          test                 4 d                      
              4 d          test                 4 d                      
              4 d          test                 4 d                      
              1 a          test                                          
              1 a          test                                          
     
    20 rows selected
     
    SQL> 
      

  8.   


    --我也测试了下,A文和C文一样,B文和D文一样
    Connected to:
    Oracle Database 10g Release 10.1.0.2.0 - ProductionSQL> select * from aa;       ID1        ID2 X
    ---------- ---------- --------------------
             1          1 a
             2          2 aa
             3          3 aaaSQL> select * from bb;       ID1        ID2 Y
    ---------- ---------- --------------------
             1          1 b
             2          2 bbSQL> select * from cc;       ID1        ID2 Z
    ---------- ---------- --------------------
             1          1 c--A文
    SQL> edit
    Wrote file afiedt.buf  1  select AA.x,BB.y,CC.z
      2    from AA,BB,CC
      3    where AA.id1 = BB.id1(+) and
      4    AA.id2 = BB.id2(+) and
      5    BB.id1 = CC.id1(+) and
      6*   BB.id2 = CC.id2(+)
    SQL> /X                    Y                    Z
    -------------------- -------------------- --------------------
    a                    b                    c
    aaa
    aa                   bb--B文
    SQL> edit
    Wrote file afiedt.buf  1  select AA.x,BB.y,CC.z
      2    from AA,BB,CC
      3    where AA.id1 = BB.id1(+) and
      4    AA.id2 = BB.id2(+) and
      5    AA.id1 = CC.id1(+) and
      6*   AA.id2 = CC.id2(+)
    SQL> /X                    Y                    Z
    -------------------- -------------------- --------------------
    a                    b                    c
    aa                   bb
    aaa--C文
    SQL> edit
    Wrote file afiedt.buf  1   select AA.x,BB.y,CC.z
      2    from AA
      3    left join BB on
      4    AA.id1 = BB.id1 and
      5    AA.id2 = BB.id2
      6    left join CC on
      7    BB.id1 = CC.id1 and
      8*   BB.id2 = CC.id2
    SQL> /X                    Y                    Z
    -------------------- -------------------- --------------------
    a                    b                    c
    aaa
    aa                   bb--D文
    SQL> edit
    Wrote file afiedt.buf  1  select AA.x,BB.y,CC.z
      2    from AA
      3    left join BB on
      4    AA.id1 = BB.id1 and
      5    AA.id2 = BB.id2
      6    left join CC on
      7    AA.id1 = CC.id1 and
      8*   AA.id2 = CC.id2
    SQL> /X                    Y                    Z
    -------------------- -------------------- --------------------
    a                    b                    c
    aa                   bb
    aaaSQL> 
      

  9.   

    A 跟C 只是写法上不同 其实是一个意思 结果是一样的  A.id=b.id(+) 老的写法  a left join b on a.id=b.id 新的  b与D一样
      

  10.   

    我知道,我只是说,ABCD文都是一样的,而且就算AA,BB,CC中有其他id1和id2不同数据,那么四个文查询结果都是一样
      

  11.   

    呵呵,其实没上面本质的区别,就像wkc168说的一样,写法上不同而已,一个是老写法,一个新写法
      

  12.   

    谢谢【minitoy】和【gelyon】的费心测试和总结O(∩_∩)O
    看来A和C的写法都没有问题,这下放心了o(≧v≦)o~~
    谢谢关注帖子的朋友