14、连接的使用

被刻印的时光 ゝ 提交于 2019-12-01 09:28:00

连接的使用:inner join(join) 、left jion 、right join

从多个数据表中读取数据

JOIN 按照功能大致分为如下三类:

  • INNER JOIN(内连接,或等值连接):获取两个表中字段匹配关系的记录。
  • LEFT JOIN(左连接):获取左表所有记录,即使右表没有对应匹配的记录。
  • RIGHT JOIN(右连接): 与 LEFT JOIN 相反,用于获取右表所有记录,即使左表没有对应匹配的记录。

两个表:

mysql> SELECT * FROM tcount_tbl;
+---------------+--------------+
| runoob_author | runoob_count |
+---------------+--------------+
| 菜鸟教程  | 10           |
| RUNOOB.COM    | 20           |
| Google        | 22           |
+---------------+--------------+
3 rows in set (0.01 sec)

mysql> SELECT * from runoob_tbl;
+-----------+---------------+---------------+-----------------+
| runoob_id | runoob_title  | runoob_author | submission_date |
+-----------+---------------+---------------+-----------------+
| 1         | 学习 PHP    | 菜鸟教程  | 2017-04-12      |
| 2         | 学习 MySQL  | 菜鸟教程  | 2017-04-12      |
| 3         | 学习 Java   | RUNOOB.COM    | 2015-05-01      |
| 4         | 学习 Python | RUNOOB.COM    | 2016-03-06      |
| 5         | 学习 C      | FK            | 2017-04-05      |
+-----------+---------------+---------------+-----------------+
5 rows in set (0.01 sec)

1、 inner join(join)

select a.runoob_id, a.runoob_author, b.runoob_count 
from runoob_tbl a join tcount_tbl b 
on a.runoob_author = b.runoob_author;

等价于:
SELECT a.runoob_id, a.runoob_author, b.runoob_count 
FROM runoob_tbl a, tcount_tbl b 
WHERE a.runoob_author = b.runoob_author;

+-----------+---------------+--------------+
| runoob_id | runoob_author | runoob_count |
+-----------+---------------+--------------+
|         1 | 菜鸟教程      |           10 |
|         2 | 菜鸟教程      |           10 |
|         3 | RUNOOB.COM    |           20 |
|         4 | RUNOOB.COM    |           20 |
+-----------+---------------+--------------+
4 rows in set

注意:
on a.runoob_author = b.runoob_author 中名字不一致时也可查询,只要记录中字段相等即可

如:

mysql> SELECT * FROM tcount_tbl;
+---------------+--------------+
| author        | runoob_count |
+---------------+--------------+
| 菜鸟教程       | 10           |
| RUNOOB.COM    | 20           |
| Google        | 22           |
+---------------+--------------+
3 rows in set (0.01 sec)

select a.runoob_id, a.runoob_author, b.runoob_count 
from runoob_tbl a join tcount_tbl b 
on a.runoob_author = b.author;

+-----------+---------------+--------------+
| runoob_id | runoob_author | runoob_count |
+-----------+---------------+--------------+
|         1 | 菜鸟教程      |           10 |
|         2 | 菜鸟教程      |           10 |
|         3 | RUNOOB.COM    |           20 |
|         4 | RUNOOB.COM    |           20 |
+-----------+---------------+--------------+
4 rows in set

2、left join

SELECT a.runoob_id, a.runoob_author, b.runoob_count 
FROM runoob_tbl a LEFT JOIN tcount_tbl b 
ON a.runoob_author = b.runoob_author;

+-----------+---------------+--------------+
| runoob_id | runoob_author | runoob_count |
+-----------+---------------+--------------+
|         1 | 菜鸟教程      |           10 |
|         2 | 菜鸟教程      |           10 |
|         3 | RUNOOB.COM    |           20 |
|         4 | RUNOOB.COM    |           20 |
|         5 | FK            | NULL         |
+-----------+---------------+--------------+
5 rows in set

3、right join

select a.runoob_id, a.runoob_author, b.runoob_count 
from runoob_tbl a right join tcount_tbl b 
on a.runoob_author = b.runoob_author;

+-----------+---------------+--------------+
| runoob_id | runoob_author | runoob_count |
+-----------+---------------+--------------+
|         1 | 菜鸟教程      |           10 |
|         2 | 菜鸟教程      |           10 |
|         3 | RUNOOB.COM    |           20 |
|         4 | RUNOOB.COM    |           20 |
| NULL      | NULL          |           22 |
+-----------+---------------+--------------+
5 rows in set

为什么在最后一行 runoob_author 中显示为 NULL,因为我们在开始选择表的内容时候选择的是a.runoob_author,在runoob_author中不存在Google,那么意味者只能用null字段来代替。

4、知识拓展

4.1、where 与 on 的区别

  • where 会将两个表进行全连接(也即是全相乘得到了临时表)之后再查找,没有利用索引来查找,效率低,数据量大。
  • on 会在根据索引一边查找一边取出数据,效率高

4.2、 on之后可以再用where进行查询

select a.runoob_id, a.runoob_author, b.runoob_count 
from runoob_tbl a right join tcount_tbl b 
on a.runoob_author = b.runoob_author
where a.runoob_id=1;

+-----------+---------------+--------------+
| runoob_id | runoob_author | runoob_count |
+-----------+---------------+--------------+
|         1 | 菜鸟教程      |           10 |
+-----------+---------------+--------------+
1 row in set
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!