文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

【MYSQL高级】Mysql 表的七种连接方式【附带练习sql】

2023-09-17 22:02

关注

文章目录

连接

七种JOIN介绍

图形连接方式说明SQL
https://img-blog.csdnimg.cn/bcd7dd0ad8cf440da09d71a38b89944a.png内连接共有部分SELECT FROM TableA A INNER JOIN TableB B ON A.Key = B.Key;
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-mQHmQOL5-1686824977738)(assets/1686824851787-1.png)]左连接A表独有+共有部分SELECT FROM TableA A LEFT JOIN TableB B ON A.Key = B.Key;
在这里插入图片描述右连接B表独有+共有部分SELECT FROM TableA A RIGHT JOIN TableB B ON A.Key = B.Key;
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-hVV4XXtO-1686824977738)(assets/1686824851787-3.png)]A的独有SELECT FROM TableA A LEFT JOIN TableB B ON A.Key = B.Key WHERE B.Key IS NULL;
在这里插入图片描述B的独有SELECT FROM TableA A RIGHT JOIN TableB B ON A.Key = B.Key WHERE A.Key IS NULL;
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-T5XPhb8d-1686824977739)(assets/1686824851787-5.png)]全连接A的独有+共有部分+B的独有SELECT FROM TableA A FULL OUTER JOIN TableB B ON A.Key = B.Key; MySQL不支持FULL OUTER JOIN这种语法
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-w50kiG6r-1686824977739)(assets/1686824851787-6.png)]A独有+B独有SELECT FROM TableA A FULL OUTER JOIN TableB B ON A.Key = B.Key WHERE A.Key IS NULL OR B.Key IS NULL; MySQL不支持FULL OUTER JOIN这种语法

练习

建表+存储数据

CREATE TABLE `tbl_emp` (`id` int(11) NOT NULL AUTO_INCREMENT,`name` varchar(20) DEFAULT NULL,`deptId` int(11) DEFAULT NULL,PRIMARY KEY (`id`) ,KEY `fk_dept_id`(`deptId`))ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8;CREATE TABLE `tbl_dept` (`id` int(11) NOT NULL AUTO_INCREMENT,`deptName` varchar(30) DEFAULT NULL,`locAdd` varchar(40) DEFAULT NULL,PRIMARY KEY (`id`)) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8;insert into tbl_dept(deptName,locAdd) values('RD',11);insert into tbl_dept(deptName,locAdd) values('HR',12);insert into tbl_dept(deptName,locAdd) values('MK',13);insert into tbl_dept(deptName,locAdd) values('MIS',14);insert into tbl_dept(deptName,locAdd) values('FD',15);insert into tbl_emp(NAME,deptId) values('z3',1);insert into tbl_emp(NAME,deptId) values('z4',1);insert into tbl_emp(NAME,deptId) values('z5',1);insert into tbl_emp(NAME,deptId) values('w5',2);insert into tbl_emp(NAME,deptId) values('w6',2);insert into tbl_emp(NAME,deptId) values('s7',3);insert into tbl_emp(NAME,deptId) values('s8',4);insert into tbl_emp(NAME,deptId) values('s9',51);

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-qUHuXxvA-1686824977740)(assets/1686824851787-7.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-8stgnQnE-1686824977740)(assets/1686824851788-8.png)]

笛卡尔积

select * from tbl_emp,tbl_dept;

5条数据8条数据两两组合,一共40条,结果如下

"id"       "name"     "deptId"    "id"      "deptName"   "locAdd""1"        "z3"        "1"        "5"        "FD"        "15""1"        "z3"        "1"        "4"        "MIS"        "14""1"        "z3"        "1"        "3"        "MK"        "13""1"        "z3"        "1"        "2"        "HR"        "12""1"        "z3"        "1"        "1"        "RD"        "11""2"        "z4"        "1"        "5"        "FD"        "15""2"        "z4"        "1"        "4"        "MIS"        "14""2"        "z4"        "1"        "3"        "MK"        "13""2"        "z4"        "1"        "2"        "HR"        "12""2"        "z4"        "1"        "1"        "RD"        "11""3"        "z5"        "1"        "5"        "FD"        "15""3"        "z5"        "1"        "4"        "MIS"        "14""3"        "z5"        "1"        "3"        "MK"        "13""3"        "z5"        "1"        "2"        "HR"        "12""3"        "z5"        "1"        "1"        "RD"        "11""4"        "w5"        "2"        "5"        "FD"        "15""4"        "w5"        "2"        "4"        "MIS"        "14""4"        "w5"        "2"        "3"        "MK"        "13""4"        "w5"        "2"        "2"        "HR"        "12""4"        "w5"        "2"        "1"        "RD"        "11""5"        "w6"        "2"        "5"        "FD"        "15""5"        "w6"        "2"        "4"        "MIS"        "14""5"        "w6"        "2"        "3"        "MK"        "13""5"        "w6"        "2"        "2"        "HR"        "12""5"        "w6"        "2"        "1"        "RD"        "11""6"        "s7"        "3"        "5"        "FD"        "15""6"        "s7"        "3"        "4"        "MIS"        "14""6"        "s7"        "3"        "3"        "MK"        "13""6"        "s7"        "3"        "2"        "HR"        "12""6"        "s7"        "3"        "1"        "RD"        "11""7"        "s8"        "4"        "5"        "FD"        "15""7"        "s8"        "4"        "4"        "MIS"        "14""7"        "s8"        "4"        "3"        "MK"        "13""7"        "s8"        "4"        "2"        "HR"        "12""7"        "s8"        "4"        "1"        "RD"        "11""8"        "s9"        "51"        "5"        "FD"        "15""8"        "s9"        "51"        "4"        "MIS"        "14""8"        "s9"        "51"        "3"        "MK"        "13""8"        "s9"        "51"        "2"        "HR"        "12""8"        "s9"        "51"        "1"        "RD"        "11"

内连接

SELECT * FROM tbl_emp a INNER JOIN tbl_dept b ON a.deptId = b.id;

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-aZ35ILZ2-1686824977740)(assets/1686824851788-9.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-sNac9oI7-1686824977741)(assets/1686824851788-10.png)]

左连接

SELECT * FROM tbl_emp a LEFT JOIN tbl_dept b ON a.deptId = b.id;

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-veaxxZmd-1686824977741)(assets/1686824851788-11.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-WbjW4vGV-1686824977741)(assets/1686824851788-12.png)]

右连接

SELECT * FROM tbl_emp a RIGHT JOIN tbl_dept b ON a.deptId = b.id;

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-u96RObbU-1686824977741)(assets/1686824851788-13.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Q9huZfmw-1686824977741)(assets/1686824851788-14.png)]

A表独有

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-hR7svfpX-1686824977742)(assets/1686824851788-15.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-BrFmCxzC-1686824977742)(assets/1686824851788-16.png)]

SELECT * FROM tbl_emp a LEFT JOIN tbl_dept b ON a.deptId = b.id WHERE b.id is null;

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-2KKU8i1R-1686824977742)(assets/1686824851789-17.png)]

B表独有

SELECT * FROM tbl_emp a RIGHT JOIN tbl_dept b ON a.deptId = b.id WHERE a.deptId is null;

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-1FUBJoXE-1686824977742)(assets/1686824851789-18.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-29w3Q3Vf-1686824977742)(assets/1686824851789-19.png)]

全连接

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-UrTh9p5N-1686824977743)(assets/1686824851789-20.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-jtIDkl7R-1686824977743)(assets/1686824851789-21.png)]

尝试换一种方式实现:A的独有+共有+B的独有

实现方式一:左连接+右连接+公共部分去重

SELECT * FROM tbl_emp a LEFT JOIN tbl_dept b ON a.deptId = b.id UNION SELECT * FROM tbl_emp a RIGHT JOIN tbl_dept b ON a.deptId = b.id;

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-LkhQ6tcO-1686824977743)(assets/1686824851789-22.png)]

实现方式二:左连接+B独有

SELECT * FROM tbl_emp a LEFT JOIN tbl_dept b ON a.deptId = b.id UNION SELECT * FROM tbl_emp a RIGHT JOIN tbl_dept b ON a.deptId = b.id WHERE a.deptId is null;

A独有+B独有

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-KdkdloFq-1686824977744)(assets/1686824851789-23.png)]

SELECT * FROM tbl_emp a LEFT JOIN tbl_dept b ON a.deptId = b.id WHERE b.id is null UNION SELECT * FROM tbl_emp a RIGHT JOIN tbl_dept b ON a.deptId = b.id WHERE a.deptId is null;

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-9dcf7yHP-1686824977744)(assets/1686824851789-24.png)]

文章说明

本文章为本人学习尚硅谷的学习笔记,文章中大部分内容来源于尚硅谷视频(点击学习尚硅谷相关课程),也有部分内容来自于自己的思考,发布文章是想帮助其他学习的人更方便地整理自己的笔记或者直接通过文章学习相关知识,如有侵权请联系删除,最后对尚硅谷的优质课程表示感谢。

来源地址:https://blog.csdn.net/laodanqiu/article/details/131233741

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-数据库
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯