刚开始接触sql server,对于外连接的概念不是很清楚,请各位达人解释一下。oracle 中的语句:select A.*
from A,B
where A.a = B.a(+)
and B.b(+) = 'XX'
and B.c(+) = 'ZZ'改在sql server中的写法是下面那样的呢?如果两个都行的话,这个左右sql server是怎么解析的?
希望能详细解答。
1,
select A.*
from A
LEFT OUTER JOIN B ON
(A.a = B.a 
 and B.b = 'XX'
 and B.c = 'ZZ'

2,
select A.*
from A
RIGHT OUTER JOIN B ON
(B.a = A.a  
 and B.b = 'XX'
 and B.c = 'ZZ'

解决方案 »

  1.   

    --左连接
    select A.* 
    from A 
    LEFT JOIN B ON A.a = B.a
    where B.b = 'XX' and B.c = 'ZZ' 
    --右连接
    select A.* 
    from A 
    RIGHT OUTER JOIN B ON B.a = A.a
    where B.b = 'XX' and B.c = 'ZZ' 
      

  2.   

      选贴看下oracle的连接,
      create   table   dali.test1(a   int,b   int);   
      create   table   dali.test2(a   int,b   int);   
        
      insert   into   dali.test1   values(1,456);   
      insert   into   dali.test1   values(2,427);   
      insert   into   dali.test2   values(1,45456);   
      insert   into   dali.test2   values(3,45656);   
        
      ---内连接   
      select   *   from   dali.test1   a,   dali.test2   b   where   a.a=b.a;   
        
      ---左连接   
      select   *   from   dali.test1   a,   dali.test2   b   where   a.a=b.a(+);   
        
      ---右连接   
      select   *   from   dali.test1   a,   dali.test2   b   where   a.a(+)=b.a;   
        
      ---完全连接   
      select   *   from   dali.test1   a,   dali.test2   b   where   a.a=b.a(+)   
      union   
      select   *   from   dali.test1   a,   dali.test2   b   where   a.a(+)=b.a;   
        
      ---迪卡尔   
      select   *   from   dali.test1,   dali.test2;
      

  3.   

    楼主那个连接应属于左连接,用:
    select A.* 
    from A 
    LEFT OUTER JOIN B ON 
    (A.a = B.a 
    and B.b = 'XX' 
    and B.c = 'ZZ' 
      

  4.   

    SQL JOIN 的用法 
    关于sql语句中的连接(join)关键字,是较为常用而又不太容易理解的关键字,下面这个例子给出了一个简单的解释 --建表table1,table2: 
    create table table1(id int,name varchar(10)) 
    create table table2(id int,score int) 
    insert into table1 select 1,'lee' 
    insert into table1 select 2,'zhang' 
    insert into table1 select 4,'wang' 
    insert into table2 select 1,90 
    insert into table2 select 2,100 
    insert into table2 select 3,70 
    如表 
    ------------------------------------------------- 
    table1 | table2 | 
    ------------------------------------------------- 
    id name |id score | 
    1 lee |1 90 | 
    2 zhang |2 100 | 
    4 wang |3 70 | 
    ------------------------------------------------- 
    以下均在查询分析器中执行 
    一、外连接 
    1.概念:包括左向外联接、右向外联接或完整外部联接 
    2.左连接:left join 或 left outer join 
    (1)左向外联接的结果集包括 LEFT OUTER 子句中指定的左表的所有行,而不仅仅是联接列所匹配的行。如果左表的某行在右表中没有匹配行,则在相关联的结果集行中右表的所有选择列表列均为空值(null)。 
    (2)sql语句 
    select * from table1 left join table2 on table1.id=table2.id 
    -------------结果------------- 
    id name id score 
    ------------------------------ 
    1 lee 1 90 
    2 zhang 2 100 
    4 wang NULL NULL 
    ------------------------------ 
    注释:包含table1的所有子句,根据指定条件返回table2相应的字段,不符合的以null显示 
    3.右连接:right join 或 right outer join 
    (1)右向外联接是左向外联接的反向联接。将返回右表的所有行。如果右表的某行在左表中没有匹配行,则将为左表返回空值。 
    (2)sql语句 
    select * from table1 right join table2 on table1.id=table2.id 
    -------------结果------------- 
    id name id score 
    ------------------------------ 
    1 lee 1 90 
    2 zhang 2 100 
    NULL NULL 3 70 
    ------------------------------ 
    注释:包含table2的所有子句,根据指定条件返回table1相应的字段,不符合的以null显示 
    4.完整外部联接:full join 或 full outer join 
    (1)完整外部联接返回左表和右表中的所有行。当某行在另一个表中没有匹配行时,则另一个表的选择列表列包含空值。如果表之间有匹配行,则整个结果集行包含基表的数据值。 
    (2)sql语句 
    select * from table1 full join table2 on table1.id=table2.id 
    -------------结果------------- 
    id name id score 
    ------------------------------ 
    1 lee 1 90 
    2 zhang 2 100 
    4 wang NULL NULL 
    NULL NULL 3 70 
    ------------------------------ 
    注释:返回左右连接的和(见上左、右连接) 
    二、内连接 
    1.概念:内联接是用比较运算符比较要联接列的值的联接 
    2.内连接:join 或 inner join 
    3.sql语句 
    select * from table1 join table2 on table1.id=table2.id 
    -------------结果------------- 
    id name id score 
    ------------------------------ 
    1 lee 1 90 
    2 zhang 2 100 
    ------------------------------ 
    注释:只返回符合条件的table1和table2的列 
    4.等价(与下列执行效果相同) 
    A:select a.*,b.* from table1 a,table2 b where a.id=b.id 
    B:select * from table1 cross join table2 where table1.id=table2.id (注:cross join后加条件只能用where,不能用on) 
    三、交叉连接(完全) 
    1.概念:没有 WHERE 子句的交叉联接将产生联接所涉及的表的笛卡尔积。第一个表的行数乘以第二个表的行数等于笛卡尔积结果集的大小。(table1和table2交叉连接产生3*3=9条记录) 
    2.交叉连接:cross join (不带条件where...) 
    3.sql语句 
    select * from table1 cross join table2 
    -------------结果------------- 
    id name id score 
    ------------------------------ 
    1 lee 1 90 
    2 zhang 1 90 
    4 wang 1 90 
    1 lee 2 100 
    2 zhang 2 100 
    4 wang 2 100 
    1 lee 3 70 
    2 zhang 3 70 
    4 wang 3 70 
    ------------------------------ 
    注释:返回3*3=9条记录,即笛卡尔积 
    4.等价(与下列执行效果相同) 
    A:select * from table1,table2   转载地址:http://blog.csdn.net/daisy_leaf/archive/2006/10/08/1325494.aspx
      

  5.   

    左外连接: 左边表的数据和右边的表进行匹配,如果左边的表和右边的表有匹配的话,那么就显示出来,如果右边的表没有和左边的表匹配的话,那么显示左
               边的表的数据,右边的数据显示null
    右外连接:反过来
      

  6.   

    感谢大家的回答,从[htl258],[qianjin036a]的回答中可以看出。下面这两种写法的结果是一样的。
    --左连接
    select A.* 
    from A 
    LEFT JOIN B ON A.a = B.a
    where B.b = 'XX' and B.c = 'ZZ' 
    --右连接
    select A.* 
    from A 
    RIGHT OUTER JOIN B ON B.a = A.a
    where B.b = 'XX' and B.c = 'ZZ' 我不明白的地方是为什么上面的写法会被理解为oracle中的:
     select A.*
     from A,B
     where A.a = B.a(+)
     and B.b(+) = 'XX'
     and B.c(+) = 'ZZ' 
    而不是
     select A.*
     from A,B
     where A.a = B.a(+)
     and B.b = 'XX'
     and B.c = 'ZZ' 
    具体来解释的话
     select A.* 
     from A 
     LEFT JOIN B ON A.a = B.a
     where B.b = 'XX' and B.c = 'ZZ' 
    中的[where B.b = 'XX' and B.c = 'ZZ' ]部分的外连接是怎么定义的。
    例如下面的语句如果用Sql server该怎么表示:
     select A.*
     from A,B
     where A.a = B.a(+)
     and B.b(+) = 'XX'
     and B.c(+) = 'YY' 
     and B.d = 'ZZ'
     and A.e(+) = 'OO'
     and A.f = 'PP'
      

  7.   

    5楼的回复解答了我一直以来关于 cross join 的疑问,呵呵,感谢了
      

  8.   

    --try:
    select A.* 
    from A left join B 
    on A.a = B.a
    where  B.b = 'XX' 
    and B.c = 'YY' 
    and B.d = 'ZZ' 
    and A.e = 'OO' 
    and A.f = 'PP'