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
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,'徐州中学');
select A.*,B.* from xin_stu_t A left join xin_teach_t B on A.relation_id = B.id
select A.*,B.* from xin_stu_t A left outer join xin_teach_t B on A.relation_id = B.id
select A.*,B.* from xin_stu_t A right join xin_teach_t B on A.relation_id = B.id
select A.*,B.* from xin_stu_t A right outer join xin_teach_t B on A.relation_id = B.id
select A.*,B.* from xin_stu_t A inner join xin_teach_t B on A.relation_id = B.id
select A.*,B.* from xin_stu_t A join xin_teach_t B on A.relation_id = B.id
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
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
select A.*,B.* from xin_stu_t A, xin_teach_t B
select A.*,B.* from xin_stu_t A, xin_teach_t B where A.relation_id = B.id
造数据
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,'徐州中学');
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;
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;
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;
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;
A left join B on A.relation_id = B.id and A.列名 = ‘’ and B.列名 = ‘’ 左表,A.列名 = ''这个条件不起作用
引用
说明:
left join on 后的 and 条件中 主表的条件不生效,从表的条件生效,并且从表先进行筛选后数据量可能变少,更便于与主表关联,有利于提高查询效率。
建议:
主表的筛选条件放在 where 中,从表的 筛选条件放在 on 后的 and 中。
左表无影响。and后的条件只对右表产生影响 (right join翻过来)
join的含义(必须返回左表的记录)了,条件不为真的就全部过滤掉。即对 join后的数据再进行过滤,过滤出只符合where后的条件。
总结:
当条件加在 left(right) join on之后,则只会影响右(左)边数据,不会影响左(右)表数据,不管条件是否成立,左(右))边数据都能正常返回;
inner join and 和 inner join where and 其查询结果基本一致;
当条件加在full on之后,则会根据and之后的条件分别影响左表或右表数据;
当条件加在where 之后则表示对关联结果再进行筛选,此时的结果将会依赖于where后边条件的真假