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 ...
随机推荐
- 【BZOJ2565】最长双回文串(回文树)
[BZOJ2565]最长双回文串(回文树) 题面 BZOJ 题解 枚举断点\(i\) 显然的,我们要求的就是以\(i\)结尾的最长回文后缀的长度 再加上以\(i+1\)开头的最长回文前缀的长度 至于最 ...
- 【LightOJ1259】Goldbach`s Conjecture(数论)
[LightOJ1259]Goldbach`s Conjecture(数论) 题面 Vjudge T组询问,每组询问是一个偶数n 验证哥德巴赫猜想 回答n=a+b 且a,b(a<=b)是质数的方 ...
- 构造方法里的super()方法
为什么经常会遇到有的构造函数会有super(),而有的却没有,其实super就比如 对数函数,log的底数为10,如果为10 ,我们可写可不写,如果不为10,那么我们就要加上底数 在子类构造方法中,s ...
- 洛谷 P1146 【硬币翻转】题解
很久很久之前做过的一道题 翻n-1枚硬币,就是有一枚不翻,也可以理解为翻一枚 直接上程序,看程序说话 #include<iostream> using namespace std; ; b ...
- NodeJS 远程连接windows 上的MongoDB
---恢复内容开始--- 在购买了腾讯云主机,部署了nodejs项目之后,发现没有mongo数据库,于是在官网上下载了最新版的mongo数据库.然后就有了下边的一系列问题. 1.先说说基础配置吧. 1 ...
- Vuejs实例-使用vue-cli创建项目
1,首先从官方网站下载安装Node.js,建议使用6.x版本,同时也会一并安装npm工具,npm>3.10以上. 2,npm安装很慢(国外服务器),所以一般推荐使用npm淘宝镜像cnpm,先安装 ...
- 在java或 js中的日期时间转换问题
1.在js中需要求的当前日期的周一和周日 var now = new Date(); // 当前日期时间对象 var date = now.getDate(); // 当前是几号:当前日期在一个月中的 ...
- 如何在MD(d)和MT(d)工程间正确分配和释放动态内存
MD(d)和MT(d) MD(d)和MT(d)是windows下VC开发的两个编译选项,表示程序的运行时库编译选项. /MT是"multithread, static version&quo ...
- ListIterator
1,ListIterator与Iterator Iterator的功能:next(),hasNext(),remove() 功能太少,因此出现了ListIterator,他的功能要比Iterator多 ...
- 18.C++-[ ]操作符使用 、函数对象与普通函数区别(详解)
在上章17.C++-string字符串类(详解)学习了string类,发现可以通过[ ]重载操作符来访问每个字符. 比如: string s="SAD"; for(int i=0, ...