SQL之Left Join 关联条件的探讨
在测试工作中,有时需要测试数据库数据经过sql计算后的结果是否满足某一功能查询得到的返回值。
针对某些需要功能需要联查多张表,此时 关联 的作用就异常重要了,而针对多表关联,其中 关联条件的重要性不言而喻,
不同的关联条件会得到不同的结果集。

废话不多说,下面开始做个实验。
建表 data_stock1, data_stock2
drop table if exists data_stock1;
drop table if exists data_stock2; -- 区分二表,通过amount取不同字段 create table data_stock1(
account varchar(20),
amount1 int(10),
init_date varchar(10)
); create table data_stock1(
account varchar(20),
amount2 int(10),
init_date varchar(10)
); insert into data_stock1(account,amount1,init_date) values('2001',200,'20170101');
insert into data_stock1 (account,amount1,init_date) values('2001',30,'20170102');
insert into data_stock1 (account,amount1,init_date) values('2002',210,'20170102');
insert into data_stock1 (account,amount1,init_date) values('2003',70,'20170102');
insert into data_stock1(account,amount1,init_date) values('2002',10,'20170101');
表data_stock1,select * from data_stock1;

表data_stock2,自行插入值吧,数据如下,select * from data_stock2;

---------------------------------------------------我是分割线-------------------------------------
一切准备就绪;下面实验开始;将两个表进行关联,关联 分为 外联,内联。
内联 inner join ,内联 的结果集 是data_stock2,data_stock1表中共同存在的结果;data in (data_stock2,data_stock1)
外联 outer join ,分为全联(full outer join),左联(left join),右联(right join),区别如下:
-- --左关联, a.account=b.account
select *
from data_stock1 a left join data_stock2 b on (a.account=b.account);

-- --左关联,a.account=b.account and a.init_date=b.init_date
select *
from data_stock1 a left join data_stock2 b on (a.account=b.account and a.init_date=b.init_date);

-- --左关联,a.account=b.account,再做聚合 求平均值
select a.account,(AVG(a.amount1)+AVG(b.amount2)) as dispersion
from data_stock1 a left join data_stock2 b on (a.account=b.account )
group by a.account;

-- --左关联,a.account=b.account,再做聚合 求平均值
select a.account,(AVG(a.amount1)+AVG(b.amount2)) as dispersion
from data_stock1 a left join data_stock2 b on (a.account=b.account and a.init_date=b.init_date)
group by a.account;

-- --右关联, a.account=b.account
select *
from data_stock1 a RIGHT JOIN data_stock2 b on (a.account=b.account);

-- --右关联,a.account=b.account and a.init_date=b.init_date
select *
from data_stock1 a RIGHT JOIN data_stock2 b on (a.account=b.account and a.init_date=b.init_date);

-- --内联, a.account=b.account
--写法1 ,select *
from data_stock1 a INNER JOIN data_stock2 b on a.account=b.account;
--写法2, select *
from data_stock1 a , data_stock2 b where a.account=b.account;
--写法3 ,select *
from data_stock1 a JOIN data_stock2 b on a.account=b.account;

-- --内联,a.account=b.account and a.init_date=b.init_date
select *
from data_stock1 a INNER JOIN data_stock2 b on (a.account=b.account and a.init_date=b.init_date);
select *
from data_stock1 a ,data_stock2 b where (a.account=b.account and a.init_date=b.init_date);

-- --右关联,a.account=b.account,再做聚合 求平均值
select a.account,(AVG(a.amount1)+AVG(b.amount2)) as dispersion
from data_stock1 a RIGHT JOIN data_stock2 b on (a.account=b.account )
group by a.account;

-- --右关联,a.account=b.account,再做聚合 求平均值,再做聚合 求平均值
select a.account,(AVG(a.amount1)+AVG(b.amount2)) as dispersion
from data_stock1 a RIGHT JOIN data_stock2 b on (a.account=b.account and a.init_date=b.init_date)
group by a.account;
注意,此时出现了 NULL账号,是因为右联的时候,结果集为NULL,而以 group by a.account做聚合,会有NULL

--- 多表关联探究
SQL之Left Join 关联条件的探讨的更多相关文章
- left join 关联条件位置
select e.last_name, e.department_id, d.department_name from hr.employees e left outer join hr.depart ...
- JOIN关联表中ON,WHERE后面跟条件的区别
select * from td left join (select case_id as sup_case_id , count(*) supervise_number from td_kcdc ...
- laravel4.2 union联合,join关联分组查询最新记录时,查询条件不对,解决方案
需求: 分组联合查询,或者最新记录. 问题: mysql分组的时候默认会查询第一条记录,存在gourp by时 order by 无效. 一般解决办法就是 ,select * from ( sele ...
- PLSQL_性能优化系列02_Oracle Join关联
2014-09-25 Created By BaoXinjian
- SQL中关于Join、Inner Join、Left Join、Right Join、Full Join、On、 Where区别
前言: 今天主要的内容是要讲解SQL中关于Join.Inner Join.Left Join.Right Join.Full Join.On. Where区别和用法,不用我说其实前面的这些基本SQL语 ...
- Oracle SQL——varchar2() 和 char()关联查询 存在空格
背景 表dbcontinfo 字段loanid,类型为varchar2(60) 表dbloanbal 字段loanid,类型为char(60) loanid字段实际长度为24位 问题 两张表dbloa ...
- LINQ TO SQL 中的join(转帖)
http://www.cnblogs.com/ASPNET2008/archive/2008/12/21/1358152.html join对于喜欢写SQL的朋友来说还是比较实用,也比较容易接受的东西 ...
- sql之left join、right join、inner join的区别
sql之left join.right join.inner join的区别 left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录 right join(右联接) 返回包括 ...
- Hibernate原生SQL查询多表关联,SQL语句要注意的问题
Hibernate原生SQL查询多表关联,SQL语句要注意的问题 @for&ever 2009-9-4 系统环境: MySQL5.1 Hibernate3.3 有如下的假定: 实体类 Ques ...
随机推荐
- 金三银四,2018最新iOS面试题,由它可以搞定面试官?
序言 这些资料,你一定会用到!我相信很多人都在说,iOS行业不好了,iOS现在行情越来越难了,失业的人比找工作的人还要多.失业即相当于转行,跳槽即相当于降低自己的身价.那么做iOS开发的你,你是否在时 ...
- HTTP架构介绍(2) 缓存
web缓存是自动复制所请求数据并将其保存在本地存储中的设备. 通过这样做, 可以实现: 减少网络流量 消除网络瓶颈 防止服务器超载 减少长距离的响应延迟 因此, 您可以清楚地说, web 缓存可提高用 ...
- SDP(12): MongoDB-Engine - Streaming
在akka-alpakka工具包里也提供了对MongoDB的stream-connector,能针对MongoDB数据库进行streaming操作.这个MongoDB-connector里包含了Mon ...
- kill 掉所有正在运行的hadoop jobs
# get list of job's process IDs JOB_LIST=$(hadoop job -list 2> /dev/null | grep job_ | awk '{prin ...
- 三方面搞定http协议之“报文模型”
关于http协议,这一块的知识其实相当大,但是作为一个前端开发者来说,我觉得只要知道三方面的内容就足矣把http协议是个什么东西解释清楚了.而这三方面,就是http的报文模型,请求方式以及状态码. 这 ...
- 自动化之路 Graphite监控上手指南
自动化运维怎能少了监控,推荐Graphite监控,下面是配置地址 http://www.infoq.com/cn/articles/graphite-intro/ Graphite官网 http:// ...
- BZOJ4825 单旋
分析:一道水题,去年考场发现了特点但是不会splay维护挂了,然后现在我写了个treap. 画一画图就可以解决这道题了,自己试一下. 代码如下: #include<bits/stdc++.h&g ...
- python全栈开发-Day3 字符串
python全栈开发-Day3 字符串 一.按照以下几个点展开字符串的学习 #一:基本使用 1. 用途 #首先字符串主要作用途径:名字,性别,国籍,地址等描述信息2.定义方式 在单引号\双引号\三引 ...
- 每个java初学者都应该搞懂的问题
对于这个系列里的问题,每个学JAVA的人都应该搞懂.当然,如果只是学JAVA玩玩就无所谓了.如果你认为自己已经超越初学者了,却不很懂这些问题,请将你自己重归初学者行列.内容均来自于CSDN的经典老贴. ...
- shell常用脚本
shell常用脚本 author:headsen chen 2017-10-17 15:36:17 个人原创,转载请注明,否则依法追究法律责任 1,vim name.grep.sh 2,cat ...