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请求经 ...
- PHP快速排序(递归)
日常的排序算法中,快速排序是其中一种.实现起来相对简单. 假设有一个数组,有若干(N)个元素(数字且无序),需要对其进行从小到大的排序. 快速排序的思路是怎么样的呢? 取一个中间值,然后,用其他数组元 ...
- python from entry to abandon2
学习Linux已经有大致两周了,依然感觉到自己仍然在运维的大门外徘徊.于是我想要找到一个在Linux之外的业余方向,可以以作为枯燥基础学习的调节.没过多久我就发现了Python可以说是钦定的选择,它作 ...
- 线上问题排查(2)——JDK内置工具
https://www.cnblogs.com/keanuyaoo/p/3253663.html 常用命令目录: jps命令(Java Virtual Machine Process Status T ...
- c# 7.1 Async Main方法
安装 .net framework sdk 7.1 新建一个 .net framework 7.1 的程序 在程序的工程文件的第一个 PropertyGroup 节点下加入以下子属性 <L ...
- Lintcode175-Revert Binary Tree-Easy
175. Invert Binary Tree Invert a binary tree. Example Example 1: Input: {1,3,#} Output: {1,#,3} Expl ...
- lambda Helper
/// <summary> /// 操作表达式共通类,条件并且,或者操作等 /// </summary> public static class PredicateBuilde ...
- Hive和HBase区别
1. 两者分别是什么? Apache Hive是一个构建在Hadoop基础设施之上的数据仓库.通过Hive可以使用HQL语言查询存放在HDFS上的数据.HQL是一种类SQL语言,这种语言最终被转化为M ...
- 使用Python访问微信
itchat是一个开源的微信个人号接口,使用它我们可以很方便的访问我们个人微信号里的信息.itchat的github地址如下: https://github.com/littlecodersh/itc ...
- linux存储管理之交换分区
交换分区管理 Swap ====================================================================================作用: ...