MySQL的左连接、右连接和全连接的实现
表student:
+----+-----------+------+
| id | name | age |
+----+-----------+------+
| 1 | Jim | 18 |
| 2 | Lucy | 16 |
| 3 | Lily | 16 |
| 4 | Lilei | 17 |
| 5 | Hanmeimei | 16 |
+----+-----------+------+
表mark:
+----+---------+-------+
| 1 | English | 90 |
| 1 | Math | 80 |
| 2 | English | 95 |
| 2 | Math | 70 |
| 3 | English | 70 |
| 3 | Math | 80 |
| 4 | English | 80 |
| 4 | Math | 80 |
| 8 | English | 90 |
| 8 | Math | 90 |
+----+---------+-------+
表info:
+----+----------+----------+
| id | city | district |
+----+----------+----------+
| 1 | nanjing | gulou |
| 2 | beijing | chaoyang |
| 3 | shanghai | pudong |
| 4 | hangzhou | xiaoshan |
| 5 | chengdu | wuhou |
| 6 | tianjing | hedong |
+----+----------+----------+
1.左连接:
(1)
SELECT student.id,mark.subject FROM student LEFT JOIN mark on student.id=mark.id;
查询结果:
+----+---------+
| id | subject |
+----+---------+
| 1 | English |
| 1 | Math |
| 2 | English |
| 2 | Math |
| 3 | English |
| 3 | Math |
| 4 | English |
| 4 | Math |
| 5 | NULL |
+----+---------+
(2)
SELECT student.id,mark.subject FROM student LEFT JOIN mark on student.id=mark.id where student.id<=4;
查询结果:
+----+---------+
| id | subject |
+----+---------+
| 1 | English |
| 1 | Math |
| 2 | English |
| 2 | Math |
| 3 | English |
| 3 | Math |
| 4 | English |
| 4 | Math |
+----+---------+
(3)
查询语句:select s.*,subject,score from student s left join mark m on s.id=m.id;
查询结果:
+----+-----------+------+---------+-------+
| id | name | age | subject | score |
+----+-----------+------+---------+-------+
| 1 | Jim | 18 | English | 90 |
| 1 | Jim | 18 | Math | 80 |
| 2 | Lucy | 16 | English | 95 |
| 2 | Lucy | 16 | Math | 70 |
| 3 | Lily | 16 | English | 70 |
| 3 | Lily | 16 | Math | 80 |
| 4 | Lilei | 17 | English | 80 |
| 4 | Lilei | 17 | Math | 80 |
| 5 | Hanmeimei | 16 | NULL | NULL |
+----+-----------+------+---------+-------+
(4)连接多个表时:
查询语句:SELECT s.*,subject,score,city,district FROM student s LEFT JOIN mark m ON s.id=m.id
LEFT JOIN info i ON s.id=i.id;
注意:要连接n个表就要有n-1个LEFT JOIN...ON 。
查询结果:
+----+-----------+-----+---------+-------+----------+----------+
| id | name | age | subject | score | city | district |
+----+-----------+-----+---------+-------+----------+----------+
| 1 | Jim | 18 | English | 90 | nanjing | gulou |
| 1 | Jim | 18 | Math | 80 | nanjing | gulou |
| 2 | Lucy | 16 | English | 95 | beijing | chaoyang |
| 2 | Lucy | 16 | Math | 70 | beijing | chaoyang |
| 3 | Lily | 16 | English | 70 | shanghai | pudong |
| 3 | Lily | 16 | Math | 80 | shanghai | pudong |
| 4 | Lilei | 17 | English | 80 | hangzhou | xiaoshan |
| 4 | Lilei | 17 | Math | 80 | hangzhou | xiaoshan |
| 5 | Hanmeimei | 16 | NULL | NULL | chengdu | wuhou |
+----+-----------+-----+---------+-------+----------+----------+
结论:左连接的结果集中包括左表(如(1)和(2)中的student)中符合where条件的所有行,如果左表中的某些行在右表中
没有与之匹配的(如student表中的id=5,name=Hanmeimei那行,在mark表并没有id=5与之匹配),则结果集中的右表中所选列
(如mark.subject)以null填充。可以有多个
2.右连接:
(1)
查询语句:SELECT student.id,mark.subject FROM student RIGHT JOIN mark on student.id=mark.id;
查询结果:
+------+---------+
| id | subject |
+------+---------+
| 1 | English |
| 1 | Math |
| 2 | English |
| 2 | Math |
| 3 | English |
| 3 | Math |
| 4 | English |
| 4 | Math |
| NULL | English |
| NULL | Math |
+------+---------+
(2)
查询语句:SELECT student.id,mark.subject FROM mark RIGHT JOIN student on student.id=mark.id;
查询结果:
+----+---------+
| id | subject |
+----+---------+
| 1 | English |
| 1 | Math |
| 2 | English |
| 2 | Math |
| 3 | English |
| 3 | Math |
| 4 | English |
| 4 | Math |
| 5 | NULL |
+----+---------+
结论:与左连接雷同。
3.MySQL不支持全外连接,所以只能采取关键字UNION来联合左、右连接的方法:
查询语句:SELECT s.*,subject,score FROM student s LEFT JOIN mark m ON s.id=m.id
UNION
SELECT s.*,subject,score FROM student s RIGHT JOIN mark m ON s.id=m.id;
查询结果:
+------+-----------+------+---------+-------+
| id | name | age | subject | score |
+------+-----------+------+---------+-------+
| 1 | Jim | 18 | English | 90 |
| 1 | Jim | 18 | Math | 80 |
| 2 | Lucy | 16 | English | 95 |
| 2 | Lucy | 16 | Math | 70 |
| 3 | Lily | 16 | English | 70 |
| 3 | Lily | 16 | Math | 80 |
| 4 | Lilei | 17 | English | 80 |
| 4 | Lilei | 17 | Math | 80 |
| 5 | Hanmeimei | 16 | NULL | NULL |
| NULL | NULL | NULL | English | 90 |
| NULL | NULL | NULL | Math | 90 |
+------+-----------+------+---------+-------+
结论:返回左右表的所有行。哪个表中没有的就用null填充。
MySQL的左连接、右连接和全连接的实现的更多相关文章
- my sql 下左连接 右链接、内连接等应用,INNER JOIN LEFT JOIN RIGHT JOIN
1.数据准备 建两个表格: create table student (idstu int, namestu ) ); ,"张三")(,"李四"),(,&quo ...
- R7—左右内全连接详解
在SQL查询中,经常会用到左连接.右连接.内连接.全连接,那么在R中如何实现这些功能,今天来讲一讲! SQL回顾 原理 # 连接可分为以下几类: 内连接.(典型的连接运算,使用像 = 或 ...
- [EF] - 全连接
在EntityFramework里有个DeflautIfEmpty方法可以用来表示数据库里的左联接或者右连接: http://msdn.microsoft.com/en-us/library/bb39 ...
- TCP全连接队列和半连接队列已满之后的连接建立过程抓包分析[转]
最近项目需要做单机100万长连接与高并发的服务器,我们开发完服务器以后,通过自己搭的高速压测框架压测服务端的时候,发生了奇怪的现象,就是服务端莫名其妙的少接收了连接,造成了数据包的丢失,通过网上查资料 ...
- 深度学习原理与框架-卷积网络细节-图像分类与图像位置回归任务 1.模型加载 2.串接新的全连接层 3.使用SGD梯度对参数更新 4.模型结果测试 5.各个模型效果对比
对于图像的目标检测任务:通常分为目标的类别检测和目标的位置检测 目标的类别检测使用的指标:准确率, 预测的结果是类别值,即cat 目标的位置检测使用的指标:欧式距离,预测的结果是(x, y, w, h ...
- CNN学习笔记:全连接层
CNN学习笔记:全连接层 全连接层 全连接层在整个网络卷积神经网络中起到“分类器”的作用.如果说卷积层.池化层和激活函数等操作是将原始数据映射到隐层特征空间的话,全连接层则起到将学到的特征表示映射到样 ...
- 五分钟带你读懂 TCP全连接队列(图文并茂)
爱生活,爱编码,微信搜一搜[架构技术专栏]关注这个喜欢分享的地方. 本文 架构技术专栏 已收录,有各种视频.资料以及技术文章. 一.问题 今天有个小伙伴跑过来告诉我有个奇怪的问题需要协助下,问题确实也 ...
- TCP半连接队列和全连接
概述 如上图所示, 在TCP三次握手中,服务器维护一个半连接队列(sync queue) 和一个全连接队列(accept queue). 当服务端接收到客户端第一次SYN握手请求时,将创建的req ...
- 【数据库】MySQL的左连接、右连接和全连接的实现
表student:+----+-----------+------+| id | name | age |+----+-----------+------+| 1 | Jim | 18 || 2 | ...
随机推荐
- mongodump 备份
规划 副本集,其中加了个隐藏节点,用来做备份,所以备份脚本直接在隐藏节点做,目前数据不大,直接本机磁盘存储,后续如果数据集大,那么在本地存最近一天的备份,远程根据需求存储几天的备份 创建备份用户 db ...
- 在ubuntu中安装psutil
环境:ubuntu 16.04 LTS + python 2.7/3.5共存 + psutil 4.3.0 1.sudo apt-get install python3-dev # 先把python3 ...
- 如何在CentOS 7中禁止IPv6
最近,我的一位朋友问我该如何禁止IPv6.在搜索了一番之后,我找到了下面的方案.下面就是在我的CentOS 7 迷你服务器禁止IPv6的方法. 你可以用两个方法做到这个. 方法 1 编辑文件/etc/ ...
- [移动端]rem适配
原理:给html根节点设置一个基础font-size值,然后页面的所有元素布局均相对于该font-size值采用rem单位设定.font-size的取值通过js计算. 但字体不用rem单位,原因如下: ...
- 【Python①】python简介,安装以及配置
今天开始学习python,将一些心得和知识点记录下来,如有疏漏或表达问题,欢迎指正.后面所有代码均为Python 3.3.2版本(运行环境:Windows7)编写. 附:2014年8月TIOBE编程语 ...
- PowerDesigner从Physical Data Model转Excel
参考资料:http://www.cnblogs.com/hggc/archive/2013/10/15/3369857.html 由于有把ER图转Excel的需求,幸运地找到一个可用脚本,稍做修改完成 ...
- HDU 1405
题意: 输入一个数n,输出它的素因子与这个素因子出现的次数. 分析: 用欧拉函数,变下形就好了,不再过多解释. 代码如下: #include <iostream> #include < ...
- 浅谈c语言的指针
对于非计算机专业的同学,c语言的指针往往就是老师的一句“指针不考“就带过了.c语言的指针号称是c语言的灵魂,是c语言中最精妙的部分. 指针本质上也是变量,也就是一段内存,只是他的特殊之处是他存储的数据 ...
- Informatica - Powercenter 英文版资料(转载)
Informatica - Powercenter 英文版资料 http://gerardnico.com/wiki/powercenter/powercenter
- Operational Amplifiers
1>.Operational Amplifiers:different from the resistor,the inductor and the capacitor,it's a multi ...