例
表a
aid adate
1 a1
2 a2
3 a3
表b
bid bdate
1 b1
2 b2
4 b4
left join:
select * from a left join b on a.aid = b.bid
首先取出a表中所有数据,然后再加上与a,b匹配的的数据
此时的取出的是:
1 a1 b1
2 a2 b2
3 a3 空字符
同样的也有right join
指的是首先取出b表中所有数据,然后再加上与a,b匹配的的数据
此时的取出的是:
1 a1 b1
2 a2 b2
4 空字符 b4
呵呵,大家千万注意,左右连接跟两个表a,b相连接,要取出id相同的字段数据是不一样的,
select * from a ,b where a.aid = b.bid这是仅取出匹配的数据.
此时的取出的是:
1 a1 b1
2 a2 b2
这里顺便说明下union 、union all
union是连接两个表的数据并排除所有重复的数据。
而union all不排除重复的数据。
----------------------------------------------------------------------------------------
数据库完全外连接
有以下的数据库: 学生表:student studentNo studentName classNo 001 fenzaiway 101 002 admin 102 003 way 103 004 haha 104 005 sss 班级表:class classNo className classNum(班级人数) 101 09软件1班 39 102 09软件2班 36 103 09软件3班 34 104 09软件4班 56 09计专5班 30 1、内连接 内连接可以称为自然连接或者普通连接 JOIN:根据检索条件,只有当连接表中的元组都满足连接条件的时候,才会把要检索的结果放到结果表中 现有一下查询语句: select studentNo,studentName,className from student,class where student,classNo = class.classNo 则查询结果为: studentNO studentName className 001 fenzaiway 09软件1班 002 admin 09软件1班 003 way 09软件1班 004 haha 09软件1班 由结果可以看出,由于studentNo = 005 没有对应的班级编号,所以不能匹配班级表中查找到相应的记录 2、左外连接 左外连接LEFT OUTER JOIN 左外连接:要进行的连接查询中,对于左边的表即使右边没有对应的查询连接条件也要把左边的结果完全查出来放在结果集中,空出来的的字段用NULL表示 查询语句: select studentNo,studentName,className from student,class LEFT OUTER class ON student.classNo = class.classNo 查询结果: studentNO studentName className 001 fenzaiway 09软件1班 002 admin 09软件1班 003 way 09软件1班 004 haha 09软件1班 004 haha NULL 3、右外连接 右外连接RIGHT OUTER JOIN 右 外连接:要进行的连接查询中,对于右边的表即使左边没有对应的查询连接条件也要把右边的结果完全查出来放在结果集中,空出来的的字段用NULL表示 查询语句: select studentNo,studentName,className from student,class RIGHT OUTER class ON student.classNo = class.classNo 查询结果: studentNO studentName className 001 fenzaiway 09软件1班 002 admin 09软件1班 003 way 09软件1班 004 haha 09软件1班 NULL NULL 09计专5班 4、完全外连接 完全外连接FULL OUTER JOIN 完全外连级:把所有表中的记录查询处理,左边或者右边空的的字段用NULL填补 查询语句: select studentNo,studentName,className from student,class FULL OUTER class ON student.classNo = class.classNo 查询结果: studentNO studentName className 001 fenzaiway 09软件1班 002 admin 09软件1班 003 way 09软件1班 004 haha 09软件1班 005 NULL NULL NULL NULL 09计专5班 简短叙述了数据库中连接查询的各种术语,不足之处请拍砖。。。 |