现在A,B两表
A:
  学生基本信息,StudentNO,StudentName
B:
  学生成绩表
  StudentNO,Score要求把两表连接起来.
注意:A表中有的记录可能B表中没有.
比如:A表:
0001 张三
0002 李四
0003 王五
0004 赵六
0005 钱七B表 
0001  90
0005  80
要求:不管什么情况,
1、A表有多少条记录,那么连接后就要有多少条记录(不能多也不能少)
2、如果有的学生没成绩,那么Score可以用 Null 代替
3、再请您把 Left outer ,right outer的区别讲一下,还有union all ,特别请告诉我,两表连接时要以A表记录为准时,要注意什么事项

解决方案 »

  1.   

    注意:我用left outer join 时数据不符合我要求,请看:语句一
    Select StudentNO from bs_Student
    --------------------------
    StudentNO       
    --------------- 
    200002000100101
    200002000100102
    200002000100103
    200501001001
    200501001004
    200501001012
    20050100105
    20050100114
    20050100115
    2005010021
    2005010022
    2005010033
    666(13 row(s) affected)语句二
    Select StudentNO,Score from cj_ScoreDetail
    -------------------------------------
    StudentNO       Score                
    --------------- -------------------- 
    200501001001    50.00
    200501001012    .00
    20050100105     .00
    20050100114     .00
    20050100115     .00
    666             .00
    200501001001    11.00
    200501001012    22.00
    20050100105     33.00
    20050100114     44.00
    20050100115     55.00
    666             66.00
    999             NULL(13 row(s) affected)
    语句三
    连接操作************************
    Select * from
    (
    Select StudentNO 
    from bs_Student a
    ) T1
    left outer join(
    Select Score,StudentNO from cj_ScoreDetail b
    ) T2
    On t1.StudentNO=t2.StudentNO---------------------------
    StudentNO       Score                StudentNO       
    --------------- -------------------- --------------- 
    200002000100101 NULL                 NULL
    200002000100102 NULL                 NULL
    200002000100103 NULL                 NULL
    200501001001    50.00                200501001001
    200501001001    11.00                200501001001
    200501001004    NULL                 NULL
    200501001012    .00                  200501001012
    200501001012    22.00                200501001012
    20050100105     .00                  20050100105
    20050100105     33.00                20050100105
    20050100114     .00                  20050100114
    20050100114     44.00                20050100114
    20050100115     .00                  20050100115
    20050100115     55.00                20050100115
    2005010021      NULL                 NULL
    2005010022      NULL                 NULL
    2005010033      NULL                 NULL
    666             .00                  666
    666             66.00                666(19 row(s) affected)
    ************************
    **************************
      

  2.   

    Left outer ,right outer 
    左联接就是显示所有左边的数据,尽管右边的在满足联接条件下没有数据,如果右边在联接条件有多条数据,则会左边的数据会重复显示;右联接反过来.