表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. CentOS_7.2编译安装PHP_5.6.20添加扩展模块

    添加ZendGuardLoader扩展: # 解压ZendGuardLoader.so到"/usr/local/php/lib/php/extensions/no-debug-non-zts ...

  2. PHP 数据安全问题总结

    总结:关键的判断,比较尽量使用=== 类型和值都比较的恒等比较 1.if($var) $var 遵循boolean 转换. 当转换为 boolean 时,以下值被认为是 FALSE: 布尔值 FALS ...

  3. java map的默认排序问题

    import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; import java.util.Tre ...

  4. java IO之AutoCloseable,Closeable和Flushable接口

    有3个接口对于流类相当重要.其中两个接口是Closeable和Flushable,它们是在java.io包中定义的,并且是由JDK5添加的.第3个接口是AutoColseable,它是由JDK7添加的 ...

  5. PHP字符串处理函数

    /* * 确定字符串长度 * int strlen(string str) * 比较两个字符串 * 1.strcmp函数对两个字符串进行二进制安全的比较,并区分大小写 * int strcmp(str ...

  6. 多线程下的for循环问题

    List<int> _ValueLis = new List<int>(); private void AddInt(int i) { _ValueLis.Add(i); } ...

  7. 打造基于CentOS7的xfce最简工作环境

    参考这里:http://blog.csdn.net/smstong/article/details/44802989没成功. 又参考这里http://jensd.be/125/linux/rhel/i ...

  8. shell 问题解决

    如果想找常用指令,请点击图片 shell 脚本中的指令,在不确定情况下,不要随意使用nohup,否则也许会造成灾难性后果,比如--内存爆掉 shell 随机函数生成 function random() ...

  9. Google 开源技术protobuf

    http://blog.csdn.net/hguisu/article/details/20721109#0-tsina-1-1601-397232819ff9a47a7b7e80a40613cfe1 ...

  10. C++ 之 新式转型操作符

    四种新式转型: const_cast.dynamic_cast.reinterpret_cast.static_cast!! 1.const_cast  :  去除常量性 2.dynamic_cast ...