两张表a和b,两张表通过id来关联
select * from a, b where a.id = b.id
现在a有10条记录,b有5条记录,我想让a的10条记录都显示出来,oralce只要写where a.id = b.id(+)就行了,sqlserver应怎么写啊??

解决方案 »

  1.   

    select * from a,b where a.id *= b.id或者select * from a left join b on a.id = b.id
      

  2.   

    关于外连接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
    ------------------------------
    注释:返回左右连接的和(见上左、右连接)
      

  3.   

    --为了与以后系统兼容,最好如下:select * 
    from a 
        left join b on a.id = b.id