left join
left join 是以A表为基础,A表即左表,B表即右表。
左表(A)的记录会全部显示,而右表(B)只会显示符合条件表达式的记录,如果在右表(B)中没有符合条件的记录,则记录不足的地方为NULL。
例如:news 与 news_category表的结构如下,news表的category_id与news_category表的id是对应关系。
news 表
id title category_id content addtime lastmodify
1 fashion news title 1 fashion news content 2015-01-01 12:00:00 2015-01-01 12:00:00
2 sport news title 2 sport news content 2015-01-01 12:00:00 2015-01-01 12:00:00
3 life news title 3 life news content 2015-01-01 12:00:00 2015-01-01 12:00:00
4 game news title 4 game news content 2015-01-01 12:00:00 2015-01-01 12:00:00
news_category 表
id name
1 fashion
2 sport
3 life
显示news表记录,并显示news的category名称,查询语句如下
select a.id,a.title,b.name as category_name,a.content,a.addtime,a.lastmodify
from news as a left join news_category as b
on a.category_id = b.id;
查询结果如下:
id title category_name content addtime lastmodify
1 fashion news title fashion fashion news content 2015-01-01 12:00:00 2015-01-01 12:00:00
2 sport news title sport sport news content 2015-01-01 12:00:00 2015-01-01 12:00:00
3 life news title life life news content 2015-01-01 12:00:00 2015-01-01 12:00:00
4 game news title NULL game news content 2015-01-01 12:00:00 2015-01-01 12:00:00
因 news_category 表没有id=4的记录,因此news 表中category_id=4的记录的category_name=NULL
使用left join, A表与B表所显示的记录数为 1:1 或 1:0,A表的所有记录都会显示,B表只显示符合条件的记录。
2.left join 右表数据不唯一解决方法
但如果B表符合条件的记录数大于1条,就会出现1:n的情况,这样left join后的结果,记录数会多于A表的记录数。
例如:member与member_login_log表的结构如下,member记录会员信息,member_login_log记录会员每日的登入记录。member表的id与member_login_log表的uid是对应关系。
member 表
id username
1 fdipzone
2 terry
member_login_log 表
id uid logindate
1 1 2015-01-01
2 2 2015-01-01
3 1 2015-01-02
4 2 2015-01-02
5 2 2015-01-03
查询member用户的资料及最后登入日期:
如果直接使用left join
select a.id, a.username, b.logindate
from member as a
left join member_login_log as b on a.id = b.uid;
因member_login_log符合条件的记录比member表多(a.id = b.uid),所以最后得出的记录为:
id username logindate
1 fdipzone 2015-01-01
1 fdipzone 2015-01-02
2 terry 2015-01-01
2 terry 2015-01-02
2 terry 2015-01-03
但这并不是我们要的结果,因此这种情况需要保证B表的符合条件的记录是空或唯一,我们可以使用group by来实现。
select a.id, a.username, b.logindate
from member as a
left join (select uid, max(logindate) as logindate from member_login_log group by uid) as b
on a.id = b.uid;
id username logindate
1 fdipzone 2015-01-02
2 terry 2015-01-03
总结:使用left join的两个表,最好是1:1 或 1:0的关系,这样可以保证A表的记录全部显示,B表显示符合条件的记录。
如果B表符合条件的记录不唯一,就需要检查表设计是否合理了。
---------------------
作者:傲雪星枫
来源:CSDN
原文:https://blog.csdn.net/fdipzone/article/details/45119551
版权声明:本文为博主原创文章,转载请附上博文链接!
left join的更多相关文章
- SQL Server-聚焦IN VS EXISTS VS JOIN性能分析(十九)
前言 本节我们开始讲讲这一系列性能比较的终极篇IN VS EXISTS VS JOIN的性能分析,前面系列有人一直在说场景不够,这里我们结合查询索引列.非索引列.查询小表.查询大表来综合分析,简短的内 ...
- SQL Server-聚焦NOT IN VS NOT EXISTS VS LEFT JOIN...IS NULL性能分析(十八)
前言 本节我们来综合比较NOT IN VS NOT EXISTS VS LEFT JOIN...IS NULL的性能,简短的内容,深入的理解,Always to review the basics. ...
- Nested Loops join时显示no join predicate原因分析以及解决办法
本文出处:http://www.cnblogs.com/wy123/p/6238844.html 最近遇到一个存储过程在某些特殊的情况下,效率极其低效, 至于底下到什么程度我现在都没有一个确切的数据, ...
- c# Enumerable中Aggregate和Join的使用
参考页面: http://www.yuanjiaocheng.net/ASPNET-CORE/asp.net-core-environment.html http://www.yuanjiaochen ...
- 超详细mysql left join,right join,inner join用法分析
下面是例子分析表A记录如下: aID aNum 1 a20050111 2 a20050112 3 a20050113 4 ...
- join Linq
List<Publisher> Publishers = new List<Publisher>(); Publisher publish1 = new Publisher() ...
- mysql join 和left join 对于索引的问题
今天遇到一个left join优化的问题,搞了一下午,中间查了不少资料,对MySQL的查询计划还有查询优化有了更进一步的了解,做一个简单的记录: select c.* from hotel_info_ ...
- BCL中String.Join的实现
在开发中,有时候会遇到需要把一个List对象中的某个字段用一个分隔符拼成一个字符串的情况.比如在SQL语句的in条件中,我们通常需要把List<int>这样的对象转换为“1,2,3”这样的 ...
- [数据库基础]——图解JOIN
阅读导航 一.概要 二.JOIN分类 三.JOIN分类详解 一.概要 JOIN对于接触过数据库的人,这个词都不陌生,而且很多人很清楚各种JOIN,还有很多人对这个理解也不是很透彻,这次就说说JOIN操 ...
- Spark join 源码跟读记录
PairRDDFunctions类提供了以下两个join接口,只提供一个参数,不指定分区函数时默认使用HashPartitioner;提供numPartitions参数时,其内部的分区函数是HashP ...
随机推荐
- tomcat+apache的集群配置
背景:项目比较大,用户较多,同一时间,用户在线人数较多,为此,整体架构是lvs(2台)+keepalived(2台)+apache(N台)+tomcat(N台) lvs负责分发请求,所有的web请求经 ...
- bzoj1911 [Apio2010]特别行动队commando
题目链接 斜率优化 #include<cstdio> #include<cstdlib> #include<string> #include<cstring& ...
- flask框架----信号
一.实例化补充 instance_path和instance_relative_config是配合来用的.这两个参数是用来找配置文件的,当用app.config.from_pyfile('settin ...
- axios、ajax、fetch三者的区别
1.ajax:是指一种创建交互式网页应用的网页开发技术,并且可以做到无需重新加载整个网页的情况下,能够更新部分网页,也叫作局部更新 优缺点: 1)局部更新 2)原生支持,不需要任何插件 3)原生支持, ...
- js中prototype,constructor的理解
连看4篇前辈的文章,记录一些知识点 Javascript继承机制的设计思想 Javascript 面向对象编程(一):封装 Javascript面向对象编程(二):构造函数的继承 Javascript ...
- D8 双连通分量
记得有个梗那一天,zw学生zzh大佬说逃不掉的路变成a不掉的题哈哈哈哈: 分离的路径: BZOJ 1718POJ 3177LUOGU 286: 思路:在同一个边双连通分量中,任意两点都有至少两条独立路 ...
- phpstorm 安装yaf代码提示文件
安装yaf代码提示:https://www.jianshu.com/p/dc719ae74f97 4.下载代码提示文件 下载地址: https://github.com/xudianyang/yaf. ...
- flask --- 02. 路由, 初始化配置,蓝图
一.Flask 路由 1.添加路由的方式 ① ② 实例: ① @app.route("/my_de") def detail() ② def detail() app.add_ur ...
- shh和maven项目报错
朋友整合ssh时突然报错, org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Cata ...
- Qt打开文件对话框同时选中多个文件或单个文件
Qt中打开单个文件 //str_path为文件路径 QString str_path = QFileDialog::getOpenFileName(this, tr("选择转码文件" ...