在MySQL中由于性能的关系,常常要将子查询(Sub-Queries)用连接(join)来却而代之,能够更好地使用表中索引提高查询效率。
下面介绍各种join的使用,先上图:
我们MySQL常用的为左连接(left join)、右连接(right join)和内连接(inner join)其他如图所示,余下的full join我们MySQL不支持,可用用左右连接和UNION做替代(下面举例介绍)。
1、先建立测试表两张:
CREATE TABLE a (
aID int( 1 ) AUTO_INCREMENT PRIMARY KEY ,
aNum char( 20 )
) ;
CREATE TABLE b(
bID int( 1 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
bName char( 20 )
) ;
2、插入测试数据:
INSERT INTO a
VALUES ( 1, 'a20050111' ) , ( 2, 'a20050112' ) , ( 3, 'a20050113' ) , ( 4, 'a20050114' ) , ( 5, 'a20050115' ) ;
INSERT INTO b
VALUES ( 1, '2006032401' ) , ( 2, '2006032402' ) , ( 3, '2006032403' ) , ( 4, '2006032404' ) , ( 8, '2006032408' ) ;
两表数据如下:
mysql> select * from a;
+-----+-----------+
| aID | aNum |
+-----+-----------+
| 1 | a20050111 |
| 2 | a20050112 |
| 3 | a20050113 |
| 4 | a20050114 |
| 5 | a20050115 |
+-----+-----------+
mysql> select * from 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 JOING 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
INNER JOIN b
ON a.aID =b.bID
等同于以下SQL句:
SELECT *
FROM a,b
WHERE 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并不以谁为基础,它只显示符合条件的记录等同于直接where条件的两表联查.
4.full join(全联接)
MySQL不支持,需要用左右连接和UNION方法替代,如图,效果为取得a表和b表数据的并集。
mysql> select * from a left join b on a.aID = b.bID union all 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 |
| 5 | a20050115 | NULL | NULL |
| 1 | a20050111 | 1 | 2006032401 |
| 2 | a20050112 | 2 | 2006032402 |
| 3 | a20050113 | 3 | 2006032403 |
| 4 | a20050114 | 4 | 2006032404 |
| NULL | NULL | 8 | 2006032408 |
+------+-----------+------+------------+
如果想求得a表和b表的差集,如图中右下角所示,则需要分别加条件。
mysql> select * from a left join b on a.aID = b.bID where b.bID is null union all select * from a right join b on a.aID = b.bID where a.aID is null ;
+------+-----------+------+------------+
| aID | aNum | bID | bName |
+------+-----------+------+------------+
| 5 | a20050115 | NULL | NULL |
| NULL | NULL | 8 | 2006032408 |
+------+-----------+------+------------+