【数据库】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 ...
- [EF] - 全连接
在EntityFramework里有个DeflautIfEmpty方法可以用来表示数据库里的左联接或者右连接: http://msdn.microsoft.com/en-us/library/bb39 ...
- R7—左右内全连接详解
在SQL查询中,经常会用到左连接.右连接.内连接.全连接,那么在R中如何实现这些功能,今天来讲一讲! SQL回顾 原理 # 连接可分为以下几类: 内连接.(典型的连接运算,使用像 = 或 ...
- 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内连接,左(外)连接,右(外)连接
用两个表(a_table.b_table),关联字段a_table.a_id和b_table.b_id来演示一下MySQL的内连接.外连接( 左(外)连接.右(外)连接.全(外)连接). MySQL版 ...
随机推荐
- git操作远程仓库
1.在本地电脑的上生成ssh-key ssh-keygen -t rsa -C "youremail@126.com" 可以在用户主目录里找到.ssh目录,里面有id_rsa和id ...
- 4-24日 collections模块 random模块 time模块 sys模块 os模块
1, collections模块 在内置数据类型(dict.list.set.tuple)的基础上,collections模块还提供了几个额外的数据类型:Counter.deque.defaultdi ...
- MD5加密文件
package cn.net.comsys.ut.util; import java.io.File;import java.io.FileInputStream;import java.io.IOE ...
- AspectJ使用的遇到的坑
1.导入包,但不是使用,会导致R文件错误 apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply pl ...
- python - 代码缩进
# -*- cording :utf-8 -*- # print absolute value of an integer a = 40 b = 1 if a >=50: print a els ...
- 20165234 《Java程序设计》第七周学习总结
第七周学习总结 教材内容学习 第十一章 JDBC与MySQL数据库 连接数据库: 1. 下载JDBC-MySQL数据库驱动 2. 加载JDBC-MySQL数据库驱动 3. 连接数据库 条件与排序查询: ...
- 关于istream_iterator<int>(cin)和istream_iterator<int>()的一点分析
最近在看STL,其中讲到容器这一部分的时候,有以下两个式子,有点疑惑: deque<) //函数声明 deque<) //定义一个容器 式子原本的含义都是要定义一个容器,容器的内容从标准输 ...
- 20165325 预备作业3 Linux安装及学习
Linux安装及学习 一.VirtualBox和Ubuntu安装 问题一:虚拟机中Ubuntu的版本 在设置虚拟机的操作系统的版本时,我发现我的电脑只能设置32-bit的Ubuntu版本.教程上选用了 ...
- 【ARTS】01_15_左耳听风-20190218~20190224
ARTS: Algrothm: leetcode算法题目 Review: 阅读并且点评一篇英文技术文章 Tip/Techni: 学习一个技术技巧 Share: 分享一篇有观点和思考的技术文章 Algo ...
- 【转】MySQL— 进阶
[转]MySQL— 进阶 目录 一.视图 二.触发器 三.函数 四.存储过程 五.事务 一.视图 视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名],用户使用时只需 ...