表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的左连接、右连接和全连接的实现的更多相关文章

  1. my sql 下左连接 右链接、内连接等应用,INNER JOIN LEFT JOIN RIGHT JOIN

    1.数据准备 建两个表格: create table student (idstu int, namestu ) ); ,"张三")(,"李四"),(,&quo ...

  2. R7—左右内全连接详解

    在SQL查询中,经常会用到左连接.右连接.内连接.全连接,那么在R中如何实现这些功能,今天来讲一讲! SQL回顾 原理 # 连接可分为以下几类: 内连接.(典型的连接运算,使用像   =   或   ...

  3. [EF] - 全连接

    在EntityFramework里有个DeflautIfEmpty方法可以用来表示数据库里的左联接或者右连接: http://msdn.microsoft.com/en-us/library/bb39 ...

  4. TCP全连接队列和半连接队列已满之后的连接建立过程抓包分析[转]

    最近项目需要做单机100万长连接与高并发的服务器,我们开发完服务器以后,通过自己搭的高速压测框架压测服务端的时候,发生了奇怪的现象,就是服务端莫名其妙的少接收了连接,造成了数据包的丢失,通过网上查资料 ...

  5. 深度学习原理与框架-卷积网络细节-图像分类与图像位置回归任务 1.模型加载 2.串接新的全连接层 3.使用SGD梯度对参数更新 4.模型结果测试 5.各个模型效果对比

    对于图像的目标检测任务:通常分为目标的类别检测和目标的位置检测 目标的类别检测使用的指标:准确率, 预测的结果是类别值,即cat 目标的位置检测使用的指标:欧式距离,预测的结果是(x, y, w, h ...

  6. CNN学习笔记:全连接层

    CNN学习笔记:全连接层 全连接层 全连接层在整个网络卷积神经网络中起到“分类器”的作用.如果说卷积层.池化层和激活函数等操作是将原始数据映射到隐层特征空间的话,全连接层则起到将学到的特征表示映射到样 ...

  7. 五分钟带你读懂 TCP全连接队列(图文并茂)

    爱生活,爱编码,微信搜一搜[架构技术专栏]关注这个喜欢分享的地方. 本文 架构技术专栏 已收录,有各种视频.资料以及技术文章. 一.问题 今天有个小伙伴跑过来告诉我有个奇怪的问题需要协助下,问题确实也 ...

  8. TCP半连接队列和全连接

    概述   如上图所示, 在TCP三次握手中,服务器维护一个半连接队列(sync queue) 和一个全连接队列(accept queue). 当服务端接收到客户端第一次SYN握手请求时,将创建的req ...

  9. 【数据库】MySQL的左连接、右连接和全连接的实现

    表student:+----+-----------+------+| id | name | age |+----+-----------+------+| 1 | Jim | 18 || 2 | ...

随机推荐

  1. Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) C. Ray Tracing

    我不告诉你这个链接是什么 分析:模拟可以过,但是好烦啊..不会写.还有一个扩展欧几里得的方法,见下: 假设光线没有反射,而是对应的感应器镜面对称了一下的话 左下角红色的地方是原始的的方格,剩下的三个格 ...

  2. Asp.net MVC过滤器的使用

    当我们网站开发到这里的时候,我们虽然已经实现了用户登录信息,用户不经过登录信息,比如:http://localhost:6941/UserInfo/Index如果我这样访问的话,他是可以进行操作的,所 ...

  3. 为OpenEdx中lms注册模块扩展字段

    最近遇到需求,需要扩展注册的字段,例如新增手机mobile字段 1.打开register.html文件   路径为\edxapp\edx-platform\lms\templates\register ...

  4. [IOS 开发]TableView如何刷新指定的cell 或section

    //一个section刷新 NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:]; [tableview reloadSections:ind ...

  5. 详解.Net消息队列(MSMQ)应用

    [IT168 技术文档]MSMQ是Windows 2000.Windows XP.Windows Server 2003的一个组件,并将继续包含在Windows Vista和以后的Windows服务器 ...

  6. java动手动脑和课后实验型问题String类型

    1.请运行以下示例代码StringPool.java,查看其输出结果.如何解释这样的输出结果?从中你能总结出什么? true true false 总结: 使用new关键字创建字符串对象时, 每次申请 ...

  7. php foreach 语法的遍历来源数组如果不是一个有效数组php会出现错误警告 Invalid argument supplied for foreach()

    在php中,foreach语法的遍历来源数组如果不是一个有效数组,php会出现错误警告 Invalid argument supplied for foreach() ,但是很多时候这个数组是取自某些 ...

  8. css3选择器详解

    css中除了早先最早的,ID选择器,class选择器一些以外在css3中新加入了新的选择器,新选择器的使用大大的方便了我们的编程,下面我就说一些css3的选择器的使用方法, p       选择了所有 ...

  9. php学习

    一.session使用: 1.所有内容页——最前面(<html>标签以前)添加以下代码: <?php if(!isset($_session)){ session_start(); ...

  10. centos 6 SSH配置Google Authentication 验证

    创建工作目录: mkdir google-authentication 1. 安装二维码生成依赖 #wget http://fukuchi.org/works/qrencode/qrencode-3. ...