Usage of join and union in mysql

Table building statement:

DROP TABLE IF EXISTS `student`;
CREATE TABLE `student`  (
  `student_id` int(11) NOT NULL AUTO_INCREMENT,
  `student_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
  `class_id` int(11) NOT NULL,
  PRIMARY KEY (`student_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 10 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of student
-- ----------------------------
INSERT INTO `student` VALUES (1, '皇后娘娘', 2);
INSERT INTO `student` VALUES (2, '智慧包', 2);
INSERT INTO `student` VALUES (3, '铁根', 2);
INSERT INTO `student` VALUES (4, '下巴', 2);
INSERT INTO `student` VALUES (5, '虞姬', 1);
INSERT INTO `student` VALUES (6, '安其拉', 3);
INSERT INTO `student` VALUES (7, '鲁班', 4);
INSERT INTO `student` VALUES (8, '芈月', 5);
INSERT INTO `student` VALUES (9, '多余同学', 6);

SET FOREIGN_KEY_CHECKS = 1;
DROP TABLE IF EXISTS `class`;
CREATE TABLE `class`  (
  `class_id` int(11) NOT NULL AUTO_INCREMENT,
  `class_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
  PRIMARY KEY (`class_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 8 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of class
-- ----------------------------
INSERT INTO `class` VALUES (1, '一班');
INSERT INTO `class` VALUES (2, '二班');
INSERT INTO `class` VALUES (3, '三班');
INSERT INTO `class` VALUES (4, '四班');
INSERT INTO `class` VALUES (5, '五班');
INSERT INTO `class` VALUES (7, '多余班级');

SET FOREIGN_KEY_CHECKS = 1;

The initial data of the two tables are as follows:

Insert picture description hereInsert picture description here

一、inner join

inner join

select * 
from student a
inner join class b
on a.class_id = b.class_id

The query results are as follows:

inner join

二、left join

left join

select * 
from student a
left join class b
on a.class_id = b.class_id

The query results are as follows:

Insert picture description here

Insert picture description here

select * 
from student a
left join class b
on a.class_id = b.class_id
where b.class_id is null

The query results are as follows:

Insert picture description here

三、right join

Insert picture description here

select * 
from student a
right join class b
on a.class_id = b.class_id

The query results are as follows:
Insert picture description here

Insert picture description here

select * 
from student a
right join class b
on a.class_id = b.class_id
where a.class_id is null

The query results are as follows:
Insert picture description here

Four, union

Since mysql does not support full outer join, union is used to achieve the expected result

Insert picture description here

select * 
from student a
left join class b
on a.class_id = b.class_id

union

select * 
from student a
right join class b
on a.class_id = b.class_id

The query results are as follows:

Insert picture description here

Insert picture description here

select * 
from student a
left join class b
on a.class_id = b.class_id
where b.class_id is null

union

select * 
from student a
right join class b
on a.class_id = b.class_id
where a.class_id is null

The query results are as follows:

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_44042316/article/details/108023738