SQL中联表查询操作(LEFT JOIN, RIGHT JOIN, INNER JOIN)

联表查询

只要两个表的公共字段有匹配值,就可根据指定方法将两个表中的记录进行组合

left join(左联接):返回包括左表中的所有记录和右表中联接字段相等的记录

right join(右联接):返回包括右表中的所有记录和左表中联接字段相等的记录

inner join(等值联接):返回两个表中联接字段相等的记录

基本语法:

select * from table1 left/right/inner join table2 on table.field1 compopr table2.field2

说明:

1. 多个数据表的联接可以类似方法进行扩展,如

select * from (table1 left join table2 on table.field1 compopr table2.field2) left join table3 on table2.field2 compopr table3.field2

或者换采用JOIN语句嵌套的方式,如

select * from table1 left join (table2 left join table 3 ) on table.field1 compopr table2.field2 AND on table2.field2 compopr table3.field2

2. 一个JOIN语句中可以联接多个ON子句, 如

select * from table1 left join table2 on table.field1 compopr table2.field2 AND on table1.field2 compopr table2.field2 OR on .....

3. 三中联接的具体实例

表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并不以谁为基础,它只显示符合条件的记录.

 还有就是inner join 可以结合where语句来使用 如: select * from A innerjoin B on A.aID = B.bID where b.bname='2006032401' 这样的话 就只会放回一条数据了



你可能感兴趣的:(SQL中联表查询操作(LEFT JOIN, RIGHT JOIN, INNER JOIN))