MySQL中 内连接 左外连接 右外连接 全外连接 交叉连接

初始化数据

create database db_stu;
use db_stu;
create table stu(
    student_id int primary key auto_increment,
    student_name varchar(20),
    student_class varchar(20)
);
insert into stu values(null, '张三', '0801');
insert into stu values(null, '李四', '0802');
insert into stu values(null, '王五', '0803');
insert into stu values(null, 'Tom', '0801');

create table sco(
    id int primary key auto_increment,
    score float,
    student_id int,
    object varchar(20)
);
insert into sco values(101, 80, 1, '语文');
insert into sco values(102, 90, 2, '语文');
insert into sco values(103, 88, 3, '语文');
student_id student_name student_class
1 张三 0801
2 李四 0802
3 王五 0803
4 Tom 0801
id score student_id object
101 80 1 语文
102 90 2 语文
103 88 3 语文

内连接

普通内连接

  • 关键字 inner join ... on
  • 内连接使用ON子句指定两个表的连接条件,WHERE子句来指定条件子句
mysql> select * from stu as a inner join sco as b;
+------------+--------------+---------------+-----+-------+------------+--------+
| student_id | student_name | student_class | id  | score | student_id | object |
+------------+--------------+---------------+-----+-------+------------+--------+
|          1 | 张三         | 0801          | 101 |    80 |          1 | 语文   |
|          1 | 张三         | 0801          | 102 |    90 |          2 | 语文   |
|          1 | 张三         | 0801          | 103 |    88 |          3 | 语文   |
|          2 | 李四         | 0802          | 101 |    80 |          1 | 语文   |
|          2 | 李四         | 0802          | 102 |    90 |          2 | 语文   |
|          2 | 李四         | 0802          | 103 |    88 |          3 | 语文   |
|          3 | 王五         | 0803          | 101 |    80 |          1 | 语文   |
|          3 | 王五         | 0803          | 102 |    90 |          2 | 语文   |
|          3 | 王五         | 0803          | 103 |    88 |          3 | 语文   |
|          4 | Tom          | 0801          | 101 |    80 |          1 | 语文   |
|          4 | Tom          | 0801          | 102 |    90 |          2 | 语文   |
|          4 | Tom          | 0801          | 103 |    88 |          3 | 语文   |
+------------+--------------+---------------+-----+-------+------------+--------+
12 rows in set (0.00 sec)
使用on语句关联两个表
mysql> select * from stu as a inner join sco as b on a.student_id = b.student_id;
+------------+--------------+---------------+-----+-------+------------+--------+
| student_id | student_name | student_class | id  | score | student_id | object |
+------------+--------------+---------------+-----+-------+------------+--------+
|          1 | 张三         | 0801          | 101 |    80 |          1 | 语文   |
|          2 | 李四         | 0802          | 102 |    90 |          2 | 语文   |
|          3 | 王五         | 0803          | 103 |    88 |          3 | 语文   |
+------------+--------------+---------------+-----+-------+------------+--------+
3 rows in set (0.00 sec)
使用where条件
mysql> select * from stu as a inner join sco as b on a.student_id = b.student_id where a.student_name = '张三';
+------------+--------------+---------------+-----+-------+------------+--------+
| student_id | student_name | student_class | id  | score | student_id | object |
+------------+--------------+---------------+-----+-------+------------+--------+
|          1 | 张三         | 0801          | 101 |    80 |          1 | 语文   |
+------------+--------------+---------------+-----+-------+------------+--------+
1 row in set (0.00 sec)

隐式内连接(用的是最多的)

  • 可以不使用inner join ... on关键字
mysql> select * from stu,sco where stu.student_id = sco.student_id;
+------------+--------------+---------------+-----+-------+------------+--------+
| student_id | student_name | student_class | id  | score | student_id | object |
+------------+--------------+---------------+-----+-------+------------+--------+
|          1 | 张三         | 0801          | 101 |    80 |          1 | 语文   |
|          2 | 李四         | 0802          | 102 |    90 |          2 | 语文   |
|          3 | 王五         | 0803          | 103 |    88 |          3 | 语文   |
+------------+--------------+---------------+-----+-------+------------+--------+
3 rows in set (0.00 sec)

外连接

左外连接(看左表,把左表所有的数据全部查询出来)

  • 关键字left [outer] join ... on, 可以不用写outer关键字
mysql> select * from stu as a left join sco as b on a.student_id = b.student_id;
+------------+--------------+---------------+------+-------+------------+--------+
| student_id | student_name | student_class | id   | score | student_id | object |
+------------+--------------+---------------+------+-------+------------+--------+
|          1 | 张三         | 0801          |  101 |    80 |          1 | 语文   |
|          2 | 李四         | 0802          |  102 |    90 |          2 | 语文   |
|          3 | 王五         | 0803          |  103 |    88 |          3 | 语文   |
|          4 | Tom          | 0801          | NULL |  NULL |       NULL | NULL   |
+------------+--------------+---------------+------+-------+------------+--------+
4 rows in set (0.00 sec)

右外连接(看右表,把右表所有的数据全部查询出来)

  • 使用关键字 right [outer] join ... on
mysql> select * from stu as a right join sco as b on a.student_id = b.student_id;
+------------+--------------+---------------+-----+-------+------------+--------+
| student_id | student_name | student_class | id  | score | student_id | object |
+------------+--------------+---------------+-----+-------+------------+--------+
|          1 | 张三         | 0801          | 101 |    80 |          1 | 语文   |
|          2 | 李四         | 0802          | 102 |    90 |          2 | 语文   |
|          3 | 王五         | 0803          | 103 |    88 |          3 | 语文   |
+------------+--------------+---------------+-----+-------+------------+--------+
3 rows in set (0.00 sec)

完全连接

MySQL不支持完全连接

交叉连接

  • 不带条件的话会返回两个表的笛卡尔积
  • 如果带on或者where语句之后, 会先生成两个表行数乘积的数据表,然后才根据条件从中选择
mysql> select * from stu as a cross join sco as b;
+------------+--------------+---------------+-----+-------+------------+--------+
| student_id | student_name | student_class | id  | score | student_id | object |
+------------+--------------+---------------+-----+-------+------------+--------+
|          1 | 张三         | 0801          | 101 |    80 |          1 | 语文   |
|          1 | 张三         | 0801          | 102 |    90 |          2 | 语文   |
|          1 | 张三         | 0801          | 103 |    88 |          3 | 语文   |
|          2 | 李四         | 0802          | 101 |    80 |          1 | 语文   |
|          2 | 李四         | 0802          | 102 |    90 |          2 | 语文   |
|          2 | 李四         | 0802          | 103 |    88 |          3 | 语文   |
|          3 | 王五         | 0803          | 101 |    80 |          1 | 语文   |
|          3 | 王五         | 0803          | 102 |    90 |          2 | 语文   |
|          3 | 王五         | 0803          | 103 |    88 |          3 | 语文   |
|          4 | Tom          | 0801          | 101 |    80 |          1 | 语文   |
|          4 | Tom          | 0801          | 102 |    90 |          2 | 语文   |
|          4 | Tom          | 0801          | 103 |    88 |          3 | 语文   |
+------------+--------------+---------------+-----+-------+------------+--------+
12 rows in set (0.00 sec)
mysql> select * from stu as a right join sco as b on a.student_id = b.student_id;
+------------+--------------+---------------+-----+-------+------------+--------+
| student_id | student_name | student_class | id  | score | student_id | object |
+------------+--------------+---------------+-----+-------+------------+--------+
|          1 | 张三         | 0801          | 101 |    80 |          1 | 语文   |
|          2 | 李四         | 0802          | 102 |    90 |          2 | 语文   |
|          3 | 王五         | 0803          | 103 |    88 |          3 | 语文   |
+------------+--------------+---------------+-----+-------+------------+--------+
3 rows in set (0.00 sec)

总结一下:
inner join ... on ... where ...
left join ... on ... where ...
right join ... on ... where ...
cross join ... on ...

你可能感兴趣的:(MySQL中 内连接 左外连接 右外连接 全外连接 交叉连接)