SSH社区架构社区SSM社区

【Mysql】Mysql的Join完全解读

2018-11-14  本文已影响7人  慕凌峰

一、用法

JOIN的含义就如英文单词“join”一样,连接两张表,大致分为内连接,外连接,右连接,左连接,自然连接。这里描述先甩出一张用烂了的图,然后插入测试数据。

join

用例表

CREATE TABLE t_blog(
        id INT PRIMARY KEY AUTO_INCREMENT,
        title VARCHAR(50),
        typeId INT
);

SELECT * FROM t_blog;
+----+-------+--------+
| id | title | typeId |
+----+-------+--------+
|  1 | aaa   |      1 |
|  2 | bbb   |      2 |
|  3 | ccc   |      3 |
|  4 | ddd   |      4 |
|  5 | eee   |      4 |
|  6 | fff   |      3 |
|  7 | ggg   |      2 |
|  8 | hhh   |   NULL |
|  9 | iii   |   NULL |
| 10 | jjj   |   NULL |
+----+-------+--------+

-- 博客的类别
CREATE TABLE t_type(
    id INT PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(20)
);

SELECT * FROM t_type;
+----+------------+
| id | name       |
+----+------------+
|  1 | C++        |
|  2 | C          |
|  3 | Java       |
|  4 | C#         |
|  5 | Javascript |
+----+------------+

1、笛卡尔积:CROSS JOIN

A 表:n条记录,B 表:m条记录===> n * m 条记录

SELECT * FROM t_blog CROSS JOIN t_type;
SELECT * FROM t_blog INNER JOIN t_type;
SELECT * FROM t_blog,t_type;
SELECT * FROM t_blog NATURE JOIN t_type;
select * from t_blog NATURA join t_type;
+----+-------+--------+----+------------+
| id | title | typeId | id | name       |
+----+-------+--------+----+------------+
|  1 | aaa   |      1 |  1 | C++        |
|  1 | aaa   |      1 |  2 | C          |
|  1 | aaa   |      1 |  3 | Java       |
|  1 | aaa   |      1 |  4 | C#         |
|  1 | aaa   |      1 |  5 | Javascript |
|  2 | bbb   |      2 |  1 | C++        |
|  2 | bbb   |      2 |  2 | C          |
|  2 | bbb   |      2 |  3 | Java       |
|  2 | bbb   |      2 |  4 | C#         |
|  2 | bbb   |      2 |  5 | Javascript |
|  3 | ccc   |      3 |  1 | C++        |
|  3 | ccc   |      3 |  2 | C          |
|  3 | ccc   |      3 |  3 | Java       |
|  3 | ccc   |      3 |  4 | C#         |
|  3 | ccc   |      3 |  5 | Javascript |
|  4 | ddd   |      4 |  1 | C++        |
|  4 | ddd   |      4 |  2 | C          |
|  4 | ddd   |      4 |  3 | Java       |
|  4 | ddd   |      4 |  4 | C#         |
|  4 | ddd   |      4 |  5 | Javascript |
|  5 | eee   |      4 |  1 | C++        |
|  5 | eee   |      4 |  2 | C          |
|  5 | eee   |      4 |  3 | Java       |
|  5 | eee   |      4 |  4 | C#         |
|  5 | eee   |      4 |  5 | Javascript |
|  6 | fff   |      3 |  1 | C++        |
|  6 | fff   |      3 |  2 | C          |
|  6 | fff   |      3 |  3 | Java       |
|  6 | fff   |      3 |  4 | C#         |
|  6 | fff   |      3 |  5 | Javascript |
|  7 | ggg   |      2 |  1 | C++        |
|  7 | ggg   |      2 |  2 | C          |
|  7 | ggg   |      2 |  3 | Java       |
|  7 | ggg   |      2 |  4 | C#         |
|  7 | ggg   |      2 |  5 | Javascript |
|  8 | hhh   |   NULL |  1 | C++        |
|  8 | hhh   |   NULL |  2 | C          |
|  8 | hhh   |   NULL |  3 | Java       |
|  8 | hhh   |   NULL |  4 | C#         |
|  8 | hhh   |   NULL |  5 | Javascript |
|  9 | iii   |   NULL |  1 | C++        |
|  9 | iii   |   NULL |  2 | C          |
|  9 | iii   |   NULL |  3 | Java       |
|  9 | iii   |   NULL |  4 | C#         |
|  9 | iii   |   NULL |  5 | Javascript |
| 10 | jjj   |   NULL |  1 | C++        |
| 10 | jjj   |   NULL |  2 | C          |
| 10 | jjj   |   NULL |  3 | Java       |
| 10 | jjj   |   NULL |  4 | C#         |
| 10 | jjj   |   NULL |  5 | Javascript |
+----+-------+--------+----+------------+

2、内连接:INNER JOIN

获得两个表的交集

SELECT * FROM t_blog INNER JOIN t_type ON t_blog.typeId=t_type.id;
SELECT * FROM t_blog,t_type WHERE t_blog.typeId=t_type.id;
SELECT * FROM t_blog STRAIGHT_JOIN t_type ON t_blog.typeId=t_type.id; --注意STRIGHT_JOIN有个下划线
SELECT * FROM t_blog JOIN t_type ON t_blog.typeId=t_type.id;

+----+-------+--------+----+------+
| id | title | typeId | id | name |
+----+-------+--------+----+------+
|  1 | aaa   |      1 |  1 | C++  |
|  2 | bbb   |      2 |  2 | C    |
|  7 | ggg   |      2 |  2 | C    |
|  3 | ccc   |      3 |  3 | Java |
|  6 | fff   |      3 |  3 | Java |
|  4 | ddd   |      4 |  4 | C#   |
|  5 | eee   |      4 |  4 | C#   |
+----+-------+--------+----+------+

3、左连接:LEFT JOIN

两个表的交集 + 左表剩下的数据

SELECT * FROM t_blog LEFT JOIN t_type ON t_blog.typeId=t_type.id;

+----+-------+--------+------+------+
| id | title | typeId | id   | name |
+----+-------+--------+------+------+
|  1 | aaa   |      1 |    1 | C++  |
|  2 | bbb   |      2 |    2 | C    |
|  7 | ggg   |      2 |    2 | C    |
|  3 | ccc   |      3 |    3 | Java |
|  6 | fff   |      3 |    3 | Java |
|  4 | ddd   |      4 |    4 | C#   |
|  5 | eee   |      4 |    4 | C#   |
|  8 | hhh   |   NULL | NULL | NULL |
|  9 | iii   |   NULL | NULL | NULL |
| 10 | jjj   |   NULL | NULL | NULL |
+----+-------+--------+------+------+

4、右连接:RIGHT JOIN

两个表的交集 + 右表剩下的数据

SELECT * FROM t_blog RIGHT JOIN t_type ON t_blog.typeId=t_type.id;

+------+-------+--------+----+------------+
| id   | title | typeId | id | name       |
+------+-------+--------+----+------------+
|    1 | aaa   |      1 |  1 | C++        |
|    2 | bbb   |      2 |  2 | C          |
|    3 | ccc   |      3 |  3 | Java       |
|    4 | ddd   |      4 |  4 | C#         |
|    5 | eee   |      4 |  4 | C#         |
|    6 | fff   |      3 |  3 | Java       |
|    7 | ggg   |      2 |  2 | C          |
| NULL | NULL  |   NULL |  5 | Javascript |
+------+-------+--------+----+------------+

5、外连接:OUTER JOIN

求两个表的并集

SELECT * FROM t_blog LEFT JOIN t_type ON t_blog.typeId=t_type.id
    UNION
    SELECT * FROM t_blog RIGHT JOIN t_type ON t_blog.typeId=t_type.id;

    +------+-------+--------+------+------------+
    | id   | title | typeId | id   | name       |
    +------+-------+--------+------+------------+
    |    1 | aaa   |      1 |    1 | C++        |
    |    2 | bbb   |      2 |    2 | C          |
    |    7 | ggg   |      2 |    2 | C          |
    |    3 | ccc   |      3 |    3 | Java       |
    |    6 | fff   |      3 |    3 | Java       |
    |    4 | ddd   |      4 |    4 | C#         |
    |    5 | eee   |      4 |    4 | C#         |
    |    8 | hhh   |   NULL | NULL | NULL       |
    |    9 | iii   |   NULL | NULL | NULL       |
    |   10 | jjj   |   NULL | NULL | NULL       |
    | NULL | NULL  |   NULL |    5 | Javascript |
    +------+-------+--------+------+------------+

6、自然连接:NATURE JOIN

SELECT * FROM t_blog NATURAL JOIN t_type;
    SELECT t_blog.id,title,typeId,t_type.name FROM t_blog,t_type WHERE t_blog.id=t_type.id;
    SELECT t_blog.id,title,typeId,t_type.name FROM t_blog INNER JOIN t_type ON t_blog.id=t_type.id;
    SELECT t_blog.id,title,typeId,t_type.name FROM t_blog INNER JOIN t_type USING(id);

    +----+-------+--------+------------+
    | id | title | typeId | name       |
    +----+-------+--------+------------+
    |  1 | aaa   |      1 | C++        |
    |  2 | bbb   |      2 | C          |
    |  3 | ccc   |      3 | Java       |
    |  4 | ddd   |      4 | C#         |
    |  5 | eee   |      4 | Javascript |
    +----+-------+--------+------------+

    SELECT * FROM t_blog NATURAL LEFT JOIN t_type;
    SELECT t_blog.id,title,typeId,t_type.name FROM t_blog LEFT JOIN t_type ON t_blog.id=t_type.id;
    SELECT t_blog.id,title,typeId,t_type.name FROM t_blog LEFT JOIN t_type USING(id);

    +----+-------+--------+------------+
    | id | title | typeId | name       |
    +----+-------+--------+------------+
    |  1 | aaa   |      1 | C++        |
    |  2 | bbb   |      2 | C          |
    |  3 | ccc   |      3 | Java       |
    |  4 | ddd   |      4 | C#         |
    |  5 | eee   |      4 | Javascript |
    |  6 | fff   |      3 | NULL       |
    |  7 | ggg   |      2 | NULL       |
    |  8 | hhh   |   NULL | NULL       |
    |  9 | iii   |   NULL | NULL       |
    | 10 | jjj   |   NULL | NULL       |
    +----+-------+--------+------------+

    SELECT * FROM t_blog NATURAL RIGHT JOIN t_type;
    SELECT t_blog.id,title,typeId,t_type.name FROM t_blog RIGHT JOIN t_type ON t_blog.id=t_type.id;
    SELECT t_blog.id,title,typeId,t_type.name FROM t_blog RIGHT JOIN t_type USING(id);

    +----+------------+-------+--------+
    | id | name       | title | typeId |
    +----+------------+-------+--------+
    |  1 | C++        | aaa   |      1 |
    |  2 | C          | bbb   |      2 |
    |  3 | Java       | ccc   |      3 |
    |  4 | C#         | ddd   |      4 |
    |  5 | Javascript | eee   |      4 |
    +----+------------+-------+--------+

USING子句

SELECT * FROM t_blog INNER JOIN t_type ON t_blog.typeId =t_type.id;
    +----+-------+--------+----+------+
    | id | title | typeId | id | name |
    +----+-------+--------+----+------+
    |  1 | aaa   |      1 |  1 | C++  |
    |  2 | bbb   |      2 |  2 | C    |
    |  7 | ggg   |      2 |  2 | C    |
    |  3 | ccc   |      3 |  3 | Java |
    |  6 | fff   |      3 |  3 | Java |
    |  4 | ddd   |      4 |  4 | C#   |
    |  5 | eee   |      4 |  4 | C#   |
    +----+-------+--------+----+------+


SELECT * FROM t_blog INNER JOIN t_type USING(typeId);
ERROR 1054 (42S22): Unknown column 'typeId' in 'from clause'
SELECT * FROM t_blog INNER JOIN t_type USING(id); 

--应为t_blog的typeId与t_type的id不同名,无法用Using,这里用id代替下。

    +----+-------+--------+------------+
    | id | title | typeId | name       |
    +----+-------+--------+------------+
    |  1 | aaa   |      1 | C++        |
    |  2 | bbb   |      2 | C          |
    |  3 | ccc   |      3 | Java       |
    |  4 | ddd   |      4 | C#         |
    |  5 | eee   |      4 | Javascript |
    +----+-------+--------+------------+
上一篇 下一篇

猜你喜欢

热点阅读