要点:left join,right join,inner join 首先有如下两个表: Student: ID(int) Name(nvarchar) 1 a 2 b 3 c 4 d 5 e 6 f Quiz: ID(int) score(int) 1 60 2 70 4 80 6 90 8 100 9 30 内连接:(inner join)包括连接表的匹配行 select Student.Name,Quiz.score from Quiz inner join Student on Student.ID=Quiz.ID Name score a 60 b 70 d 80 f 90 左连接:(left join)包括连接表匹配行以及左连接表的所有行 select Student.Name,Quiz.score from Student left join Quiz on Student.ID=Quiz.ID Name score a 60 b 70 c null d 80 e null f 90 右连接:(right join)结果包括连接表的匹配行以及右连接表的所有行 select Student.Name,Quiz.score from Student right join Quiz on Student.ID=Quiz.ID Name score a 60 b 70 d 80 f 90 null 100 null 30 当然,也可以看出左连接也可以写成右连接的形式: select Student.Name,Quiz.score from Student right join Quiz on Student.ID=Quiz.ID等价于 select Student.Name,Quiz.score from Quiz left join Student on Student.ID=Quiz.ID
首先有如下两个表:
Student: ID(int) Name(nvarchar)
1 a
2 b
3 c
4 d
5
e
6 f
Quiz:
ID(int) score(int)
1 60
2 70
4 80
6 90
8 100
9 30
内连接:(inner join)包括连接表的匹配行
select Student.Name,Quiz.score from Quiz inner join Student on Student.ID=Quiz.ID
Name score
a 60
b 70
d 80
f 90
左连接:(left join)包括连接表匹配行以及左连接表的所有行
select Student.Name,Quiz.score from Student left join Quiz on Student.ID=Quiz.ID
Name score
a 60
b 70
c null
d 80
e null
f 90
右连接:(right join)结果包括连接表的匹配行以及右连接表的所有行
select Student.Name,Quiz.score from Student right join Quiz on Student.ID=Quiz.ID
Name score
a 60
b 70
d 80
f 90
null 100
null 30
当然,也可以看出左连接也可以写成右连接的形式:
select Student.Name,Quiz.score from Student right join Quiz on Student.ID=Quiz.ID等价于
select Student.Name,Quiz.score from Quiz left join Student on Student.ID=Quiz.ID
--FULL JOIN
/*
FULL JOIN就是把两个表完全连起来,不管对应的连接值是否存在
*/
--CROSS JOIN
/*
CROSS JOIN按笛卡尔积连接,也就是两表组合的任何形式
*/
表A记录如下:
aID aNum
1 a20050111
2 a20050112
3 a20050113
4 a20050114
5 a20050115表B记录如下:
bID bName
1 2006032401
2 2006032402
3 2006032403
4 2006032404
8 2006032408实验如下:
1. left join
sql语句如下:
SELECT * FROM A
LEFT JOIN B
ON A.aID = B.bID结果如下:
aID aNum bID bName
1 a20050111 1 2006032401
2 a20050112 2 2006032402
3 a20050113 3 2006032403
4 a20050114 4 2006032404
5 a20050115 NULL NULL
(所影响的行数为 5 行)结果说明:
left join是以A表的记录为基础的,A可以看成左表,B可以看成右表,left join是以左表为准的.
换句话说,左表(A)的记录将会全部表示出来,而右表(B)只会显示符合搜索条件的记录(例子中为: A.aID = B.bID).
B表记录不足的地方均为NULL.2. right join
sql语句如下:
SELECT * FROM A
RIGHT JOIN B
ON A.aID = B.bID结果如下:
aID aNum bID bName
1 a20050111 1 2006032401
2 a20050112 2 2006032402
3 a20050113 3 2006032403
4 a20050114 4 2006032404
NULL NULL 8 2006032408
(所影响的行数为 5 行)结果说明:
仔细观察一下,就会发现,和left join的结果刚好相反,这次是以右表(B)为基础的,A表不足的地方用NULL填充.3.inner join
sql语句如下:
SELECT * FROM A
INNERJOIN B
ON A.aID = B.bID结果如下:
aID aNum bID bName
1 a20050111 1 2006032401
2 a20050112 2 2006032402
3 a20050113 3 2006032403
4 a20050114 4 2006032404结果说明:
很明显,这里只显示出了 A.aID = B.bID的记录.这说明inner join并不以谁为基础,它只显示符合条件的记录.-----------------[以下为网上的一点资料]------------------
LEFT JOIN操作用于在任何的 FROM 子句中,组合来源表的记录。使用 LEFT JOIN 运算来创建一个左边外部联接。左边外部联接将包含了从第一个(左边)开始的两个表中的全部记录,即使在第二个(右边)表中并没有相符值的记录。语法:
FROM table1 LEFT JOIN table2 ON table1.field1 compopr table2.field2说明:
① table1, table2参数用于指定要将记录组合的表的名称。
② field1, field2参数指定被联接的字段的名称。且这些字段必须有相同的数据类型及包含相同类型的数据,但它们不需要有相同的名称。
③ compopr参数指定关系比较运算符:"=", "<", ">", "<=", ">=" 或 "<>"。
④ 如果在INNER JOIN操作中要联接包含Memo 数据类型或 OLE Object 数据类型数据的字段,将会发生错误。