网站首页 > 技术文章 正文
【死记硬背】
MySQL的7种join操作分别是:内连接(inner join)、左连接(left join)、右连接(right join)、外连接(outer join)、左内连接(left join excluding inner join)、右内连接(right join excluding inner join)、外内连接(outer join excluding inner join)。
下面进行实际操作来看下这7种join的操作结果。
1 SQL准备
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
INSERT INTO `user` VALUES (1, '张三');
INSERT INTO `user` VALUES (2, '李四');
INSERT INTO `user` VALUES (3, '王二');
CREATE TABLE `user2` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
INSERT INTO `user2` VALUES (1, '张三');
INSERT INTO `user2` VALUES (2, '李四');
INSERT INTO `user2` VALUES (4, '麻子');
2 inner join(内连接)
select a.id,a.name from user a inner join user2 b on a.id=b.id;
3 left join(左连接)
select a.id,a.name from user a left join user2 b on a.id=b.id;
4 right join(右连接)
select b.id,b.name from user a right join user2 b on a.id=b.id;
5 outer join(外连接)
select a.id,a.name from user a left join user2 b on a.id=b.id union select b.id,b.name from user a right join user2 b on a.id=b.id;
6 left join excluding inner join(左内连接)
select a.id,a.name from user a left join user2 b on a.id=b.id where b.id is null;
7 right join excluding inner join(右内连接)
select b.id,b.name from user a right join user2 b on a.id=b.id where a.id is null;
8 outer join excluding inner join(外内连接)
select a.id,a.name from user a left join user2 b on a.id=b.id where b.id is null
union
select b.id,b.name from user a right join user2 b on a.id=b.id where a.id is null;
【温馨提示】
点赞+收藏文章,关注我并私信回复【面试题解析】,即可100%免费领取楼主的所有面试题资料!
猜你喜欢
- 2024-11-16 从阿里手册引出的Join查询思考(阿里技术手册)
- 2024-11-16 Python中join()方法——字符串、路径拼接的常用方法
- 2024-11-16 oracle 多表连接查询 join(一)(oracle多表联查效率优化)
- 2024-11-16 让程序员头疼的微服务下数据聚合join(一)
- 2024-11-16 神奇的 SQL 之 联表细节 → MySQL JOIN 的执行过程你是否知道?
- 2024-11-16 MySQL的Join到底能不能用,一文搞懂它
- 2024-11-16 一文看懂Mycat跨分片Join实现--全局表、ER分片、HBT、sharejoin
- 2024-11-16 为什么大厂不建议使用多表join?(为什么要多表查询)
- 2024-11-16 SQL Server 2012 高级用法(三)之Join
- 2024-11-16 什么,LEFT JOIN 会变成 JOIN?(left join作用)
- 标签列表
-
- content-disposition (47)
- nth-child (56)
- math.pow (44)
- 原型和原型链 (63)
- canvas mdn (36)
- css @media (49)
- promise mdn (39)
- readasdataurl (52)
- if-modified-since (49)
- css ::after (50)
- border-image-slice (40)
- flex mdn (37)
- .join (41)
- function.apply (60)
- input type number (64)
- weakmap (62)
- js arguments (45)
- js delete方法 (61)
- blob type (44)
- math.max.apply (51)
- js (44)
- firefox 3 (47)
- cssbox-sizing (52)
- js删除 (49)
- js for continue (56)
- 最新留言
-