Mysql表关联简单介绍(inner join、left join、right join、full join不支持、笛卡尔积)

文章目录

  • 0. 交集、并集、差集含义说明
  • 1. 简单演示上图七种情况
    • 0. A、B表数据准备
    • 1. left outer join 简称 left join 左表所有数据,右表关联数据,没有的以null填充
    • 2. right outer join 简称 right join,右表所有数据,左表关联数据,没有的以null填充
    • 3. inner join 简称 join 交集
    • 4. A left join B where B.key is null,A、B的差集是 A-B = 1
    • 5. B right join where A.key is null,B、A的差集是 B-A = 4
    • 6、7. full outer join 简写为full join(mysql不支持,oracle可以)
  • 2. 笛卡尔积 A * B
    • 1. 不带连接条件的笛卡尔积
    • 2. 带连接条件的笛卡尔积变inner join
  • 3. inner/left join重复数据,查询会扩散,造成笛卡尔积现象
    • 3.1 inner join 重复数据 结果集会扩散
    • 3.2 inner join 重复数据,结果集去重后同inner join不重复数据
    • 3.3 left join 重复数据 结果集会扩散
    • 3.4 left join 重复数据,结果集去重后同left join不重复数据

Mysql表关联简单介绍(inner join、left join、right join、full join不支持、笛卡尔积)_第1张图片

0. 交集、并集、差集含义说明

A 是1、2、3
B是2、3、4
A、B的交集是A∩B = 2、3
A、B的并集是 AUB = 1、2、3、4
A、B的差集是 A-B = 1
B、A的差集是 B-A = 4

1. 简单演示上图七种情况

0. A、B表数据准备

CREATE TABLE `xin_stu_t` (
  `id` bigint NOT NULL AUTO_INCREMENT COMMENT '主键',
  `relation_id` bigint DEFAULT NULL COMMENT '外键, 记录教师id',
  `student_name` varchar(30) DEFAULT NULL COMMENT '姓名',
  `student_age` bigint DEFAULT NULL COMMENT '年龄',
  `school` varchar(300) DEFAULT NULL COMMENT '学校',
  PRIMARY KEY (`id`) USING BTREE,
  KEY `xin_s_relation_id` (`relation_id`),
  KEY `xin_s_student_name` (`student_name`)
) ENGINE=InnoDB AUTO_INCREMENT=18 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='学生表';

CREATE TABLE `xin_teach_t` (
  `id` bigint NOT NULL AUTO_INCREMENT COMMENT '主键',
  `teacher_name` varchar(30) DEFAULT NULL COMMENT '教师姓名',
  `teacher_age` bigint DEFAULT NULL COMMENT '教师年龄',
  `school` varchar(300) DEFAULT NULL COMMENT '学校',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='教师表';

INSERT INTO lelele.xin_stu_t (relation_id,student_name,student_age,school) VALUES
	 (NULL,'尤仁义1',11,'徐州中学'),
	 (1,'尤仁义2',12,'徐州中学'),
	 (NULL,'朱有理1',11,'徐州中学'),
	 (2,'朱有理2',12,'徐州中学'),
	 (2,'朱有理3',13,'徐州中学'),
	 (3,'宋昆明1',11,'徐州中学'),
	 (3,'宋昆明2',12,'徐州中学'),
	 (9,'宋昆明3',13,'徐州中学');

INSERT INTO lelele.xin_teach_t (teacher_name,teacher_age,school) VALUES
	 ('王翠花1',31,'徐州中学'),
	 ('王翠花2',31,'徐州中学'),
	 ('王翠花3',33,'徐州中学'),
	 ('王翠花4',34,'徐州中学'),
	 ('王翠花5',35,'徐州中学');
	 

Mysql表关联简单介绍(inner join、left join、right join、full join不支持、笛卡尔积)_第2张图片

Mysql表关联简单介绍(inner join、left join、right join、full join不支持、笛卡尔积)_第3张图片

Mysql表关联简单介绍(inner join、left join、right join、full join不支持、笛卡尔积)_第4张图片

1. left outer join 简称 left join 左表所有数据,右表关联数据,没有的以null填充

select A.*,B.* from xin_stu_t A left join xin_teach_t B on A.relation_id = B.id

Mysql表关联简单介绍(inner join、left join、right join、full join不支持、笛卡尔积)_第5张图片

select A.*,B.* from xin_stu_t A left outer join xin_teach_t B on A.relation_id = B.id

Mysql表关联简单介绍(inner join、left join、right join、full join不支持、笛卡尔积)_第6张图片

2. right outer join 简称 right join,右表所有数据,左表关联数据,没有的以null填充

select A.*,B.* from xin_stu_t A right join xin_teach_t B on A.relation_id = B.id

Mysql表关联简单介绍(inner join、left join、right join、full join不支持、笛卡尔积)_第7张图片

select A.*,B.* from xin_stu_t A right outer join xin_teach_t B on A.relation_id = B.id

Mysql表关联简单介绍(inner join、left join、right join、full join不支持、笛卡尔积)_第8张图片

3. inner join 简称 join 交集

select A.*,B.* from xin_stu_t A inner join xin_teach_t B on A.relation_id = B.id

Mysql表关联简单介绍(inner join、left join、right join、full join不支持、笛卡尔积)_第9张图片

select A.*,B.* from xin_stu_t A join xin_teach_t B on A.relation_id = B.id

Mysql表关联简单介绍(inner join、left join、right join、full join不支持、笛卡尔积)_第10张图片

4. A left join B where B.key is null,A、B的差集是 A-B = 1

 select A.*,B.* from xin_stu_t A left join xin_teach_t B on A.relation_id = B.id where B.id is null

Mysql表关联简单介绍(inner join、left join、right join、full join不支持、笛卡尔积)_第11张图片

5. B right join where A.key is null,B、A的差集是 B-A = 4

select A.*,B.* from xin_stu_t A right join xin_teach_t B on A.relation_id = B.id where A.id is null

Mysql表关联简单介绍(inner join、left join、right join、full join不支持、笛卡尔积)_第12张图片

6、7. full outer join 简写为full join(mysql不支持,oracle可以)

Mysql表关联简单介绍(inner join、left join、right join、full join不支持、笛卡尔积)_第13张图片

2. 笛卡尔积 A * B

1. 不带连接条件的笛卡尔积

select A.*,B.* from xin_stu_t A, xin_teach_t B

Mysql表关联简单介绍(inner join、left join、right join、full join不支持、笛卡尔积)_第14张图片
Mysql表关联简单介绍(inner join、left join、right join、full join不支持、笛卡尔积)_第15张图片

2. 带连接条件的笛卡尔积变inner join

select A.*,B.* from xin_stu_t A, xin_teach_t B where A.relation_id = B.id

Mysql表关联简单介绍(inner join、left join、right join、full join不支持、笛卡尔积)_第16张图片

3. inner/left join重复数据,查询会扩散,造成笛卡尔积现象

造数据

drop table if exists  xin_stu_t_bak;

drop table if exists xin_teach_t_bak;

create table `xin_stu_t_bak` (
  `id` bigint not null COMMENT '主键',
  `relation_id` bigint default null COMMENT '外键, 记录教师id',
  `student_name` varchar(30) default null COMMENT '姓名',
  `student_age` bigint default null COMMENT '年龄',
  `school` varchar(300) default null COMMENT '学校'
);

CREATE TABLE `xin_teach_t_bak` (
  `id` bigint NOT NULL COMMENT '主键',
  `teacher_name` varchar(30) DEFAULT NULL COMMENT '教师姓名',
  `teacher_age` bigint DEFAULT NULL COMMENT '教师年龄',
  `school` varchar(300) DEFAULT NULL COMMENT '学校'
);

insert into xin_stu_t_bak select * from  xin_stu_t;

insert into xin_teach_t_bak select * from  xin_teach_t;

INSERT INTO lelele.xin_teach_t_bak (id,teacher_name,teacher_age,school) values (1,'王翠花1',31,'徐州中学');

INSERT INTO lelele.xin_teach_t_bak (id,teacher_name,teacher_age,school) VALUES (1,'王翠花1',31,'徐州中学');

INSERT INTO lelele.xin_teach_t_bak (id,teacher_name,teacher_age,school) VALUES (2,'王翠花2',31,'徐州中学');

3.1 inner join 重复数据 结果集会扩散

select A.,B. from xin_stu_t_bak A inner join xin_teach_t_bak B on A.relation_id = B.id order by A.id;

Mysql表关联简单介绍(inner join、left join、right join、full join不支持、笛卡尔积)_第17张图片

3.2 inner join 重复数据,结果集去重后同inner join不重复数据

select distinct A.,B. from xin_stu_t_bak A inner join xin_teach_t_bak B on A.relation_id = B.id order by A.id;

Mysql表关联简单介绍(inner join、left join、right join、full join不支持、笛卡尔积)_第18张图片

3.3 left join 重复数据 结果集会扩散

select A.,B. from xin_stu_t_bak A left join xin_teach_t_bak B on A.relation_id = B.id order by A.id;

Mysql表关联简单介绍(inner join、left join、right join、full join不支持、笛卡尔积)_第19张图片

3.4 left join 重复数据,结果集去重后同left join不重复数据

select distinct A.,B. from xin_stu_t_bak A left join xin_teach_t_bak B on A.relation_id = B.id order by A.id;
Mysql表关联简单介绍(inner join、left join、right join、full join不支持、笛卡尔积)_第20张图片

你可能感兴趣的:(Mysql,mysql,数据库)