在测试工作中,有时需要测试数据库数据经过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 关联条件的探讨的更多相关文章

  1. left join 关联条件位置

    select e.last_name, e.department_id, d.department_name from hr.employees e left outer join hr.depart ...

  2. JOIN关联表中ON,WHERE后面跟条件的区别

    select * from td  left join (select case_id as sup_case_id , count(*) supervise_number from  td_kcdc ...

  3. laravel4.2 union联合,join关联分组查询最新记录时,查询条件不对,解决方案

    需求: 分组联合查询,或者最新记录. 问题:  mysql分组的时候默认会查询第一条记录,存在gourp by时 order by 无效. 一般解决办法就是 ,select * from ( sele ...

  4. PLSQL_性能优化系列02_Oracle Join关联

    2014-09-25 Created By BaoXinjian

  5. 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语 ...

  6. Oracle SQL——varchar2() 和 char()关联查询 存在空格

    背景 表dbcontinfo 字段loanid,类型为varchar2(60) 表dbloanbal 字段loanid,类型为char(60) loanid字段实际长度为24位 问题 两张表dbloa ...

  7. LINQ TO SQL 中的join(转帖)

    http://www.cnblogs.com/ASPNET2008/archive/2008/12/21/1358152.html join对于喜欢写SQL的朋友来说还是比较实用,也比较容易接受的东西 ...

  8. sql之left join、right join、inner join的区别

    sql之left join.right join.inner join的区别 left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录 right join(右联接) 返回包括 ...

  9. Hibernate原生SQL查询多表关联,SQL语句要注意的问题

    Hibernate原生SQL查询多表关联,SQL语句要注意的问题 @for&ever 2009-9-4 系统环境: MySQL5.1 Hibernate3.3 有如下的假定: 实体类 Ques ...

随机推荐

  1. js中url跳转问题

    问题描述:列表中有不同的企业名字,每个企业名字都有一个不同的链接,用id做为参数区分.点击不同的名字,根据id的不同跳转到对应的详情页,设置连接如下: 1. url = http://localhos ...

  2. php 创建和修改文件内容

    file_put_contents写入文件 我们先来学习第一种写入文件的方式: int file_put_contents ( string $文件路径, string $写入数据]) 功能:向指定的 ...

  3. 在 HTML5 中捕获音频和视频

    简介 长久以来,音频/视频捕获都是网络开发中的"圣杯".多年来,我们总是依赖于浏览器插件(Flash 或 Silverlight)实现这一点.快来看看吧! 现在轮到 HTML5 大 ...

  4. R实战 第五篇:绘图(ggplot2)

    ggplot2包实现了基于语法的.连贯一致的创建图形的系统,由于ggplot2是基于语法创建图形的,这意味着,它由多个小组件构成,通过底层组件可以构造前所未有的图形.ggplot2可以把绘图拆分成多个 ...

  5. WCF使用纯代码的方式进行服务寄宿

    服务寄宿的目的是为了开启一个进程,为WCF服务提供一个运行的环境.通过为服务添加一个或者多个终结点,使之暴露给潜在的服务消费,服务消费者通过匹配的终结点对该服务进行调用,除去上面的两种寄宿方式,还可以 ...

  6. 1111 WordReplace

    #include<iostream> #include<string> using namespace std; int main() { string sa,sb,s; wh ...

  7. The method queryForMap(String, Object...) from the type JdbcTemplate refers to the missing type DataAccessException

    Add spring-tx jar of your spring version to your classpath.

  8. 原生js怎样获取后台端口数据

    全称: XMLHttpReques <!DOCTYPE html> <html lang="en"> <head> <meta chars ...

  9. Spring源码学习:第0步--环境准备

    Spring源码现在已托管于GitHub,相比于以前直接从官网下载一个压缩包的方式来说,确实方便了不少. GitHub地址:https://github.com/spring-projects/spr ...

  10. CVPR2018: Unsupervised Cross-dataset Person Re-identification by Transfer Learning of Spatio-temporal Patterns

    论文可以在arxiv下载,老板一作,本人二作,也是我们实验室第一篇CCF A类论文,这个方法我们称为TFusion. 代码:https://github.com/ahangchen/TFusion 解 ...