侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

MySQL中union和join语句使用区别的辨析教程

数据库  /  管理员 发布于 6年前   214

union和join是需要联合多张表时常见的关联词,具体概念我就不说了,想知道上网查就行,因为我也记不准确。
先说差别:union对两张表的操作是合并数据条数,等于是纵向的,要求是两张表字段必须是相同的(Schema of both sides of union should match.)。也就是说如果A表中有三条数据,B表中有两条数据,那么A union B就会有五条数据。说明一下union 和union all的差别,对于union如果存在相同的数据记录会被合并,而union all不会合并相同的数据记录,该有多少条记录就会有多少条记录。例如在mysql下执行以下语句:

select * from tmp_libingxue_a;name numberlibingxue 1001yuwen 1002select * from tmp_libingxue_b;name numberlibingxue 1001feiyao 1003select * from tmp_libingxue_a union select * from tmp_libingxue_b;libingxue 1001yuwen 1002feiyao 1003select * from tmp_libingxue_a union all select * from tmp_libingxue_b;libingxue 1001yuwen 1002libingxue 1001feiyao 1003

但是这样在hive里面是不能执行的,执行select * from tmp_libingxue_a union all select * from tmp_libingxue_b;会failed,hive中union必须在子查询中进行。如

select * from (select * from tmp_yuwen_a union all select * from tmp_yuwen_b) t1;

注意,必须是union all,单独用union它会提示你缺少ALL,而且后面的t1必须写,你可以写成a或者b,但是一定要写,不写会出错。
而join则是偏于横向的联合,仅仅是偏向于,等下详细说明。join跟union比起来显得更宽松,对两个表的字段不做要求,没有限制条件的join等于两个表的笛卡尔乘积,所有join需要有限制条件来约束,经过限制的join就是横向的扩张了。对于满足限制条件的join会被提取出来,不满足的直接过滤掉。用法可以很灵活,下面有两个简单的例子:

select * from (select * from tmp_yuwen_a)t1 join (select * from tmp_yuwen_b) t2;select * from tmp_yuwen_a t1 join (select * from tmp_yuwen_b) t2; 

left outer join和right outer join用法类似,区别就是left outer join会把左边表的字段全部选择出来,右边表的字段把符合条件的也选择出来,不满足的全部置空,也就是说以左边表为参照。right outer join同理以右边表为参照。这三个join之间的差别说过很多次,网上也有更详细的解释,不再赘述。
相同点:在某些特定的情况下,可以用join实现union all的功能,这种情况是有条件的,当出现这种情况的时候选择union all还是group by就可以看情况或者看两者的消耗而决定。sql虽然就在那么几个关键词,但变化多端、功能强大,只要能实现想要的功能,怎么用随便你。需求情况sql简单重现如下

drop table tmp_libingxue_resource;create external table if not exists tmp_libingxue_resource(  user_id string,  shop_id string,  auction_id  string,  search_time  string)partitioned by (pt string)row format delimited fields terminated by '\t'lines terminated by '\n'stored as sequencefile;drop table tmp_libingxue_result;create external table if not exists tmp_libingxue_result(  user_id string,  shop_id string,  auction_id  string,  search_time  string)partitioned by (pt string)row format delimited fields terminated by '\t'lines terminated by '\n'stored as sequencefile;insert overwrite table tmp_libingxue_result where(pt=20041104) select * from tmp_libingxue_resource;
sudo -u taobao hadoop dfs -rmr /group/tbads/warehouse/tmp_libingxue_result/pt=20041104sudo -u taobao hadoop jar /home/taobao/dataqa/framework/DailyReport.jar com.alimama.loganalyzer.tool.SeqFileLoader tmp_libingxue_resource.txt hdfs://v039182.sqa.cm4:54310/group/tbads/warehouse/tmp_libingxue_result/pt=20041104/part-00000 
hive> select * from tmp_libingxue_resource;
OK2001 0  11  101  200411042002 0  11  102  20041104
hive> select * from tmp_libingxue_result;
OK2001 0  12  103  200411042002 0  12  104  20041104
select user_id,shop_id,max(auction_id),max(search_time)from(select * from tmp_libingxue_resource union allselect * from tmp_libingxue_result )t1group by user_id,shop_id;
2001 0  12  1032002 0  12  104
select t1.user_id,t1.shop_id,t2.auction_id,t2.search_timefrom(select * from tmp_libingxue_resource) t1join(select * from tmp_libingxue_result) t2on t1.user_id=t2.user_id and t1.shop_id=t2.shop_id;
2001 0  12  1032002 0  12  104


通过前面的介绍,使用UNION对表的结果集进行并运算与使用JOIN对多表进行连接,二者有本质的不同。
下面给出一个使用UNION运算符连接二表记录的运算实例。
典型的二表记录的UNION运算

假定有两个表Table3和Table4,其包含的列和数据分别如下所示。

Table1数据库表

Table2数据库表

Table1表和Table2表具有相同的列结构,因此可以使用UNION运算符连接两个表的记录集,得到的连接结果如下表所示。

使用UNION连接Table3表和Table4表的记录

上述连接过程的实现代码可表示如下:

SELECT *FROM Table1UNIONSELECT *FROM Table2


  • 上一条:
    MySQL中UPDATE与DELETE语句的使用教程
    下一条:
    MySQL中join语句的基本使用教程及其字段对性能的影响
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 分库分表的目的、优缺点及具体实现方式介绍(0个评论)
    • DevDB - 在 VS 代码中直接访问数据库(0个评论)
    • 在ubuntu系统中实现mysql数据存储目录迁移流程步骤(0个评论)
    • 在mysql中使用存储过程批量新增测试数据流程步骤(0个评论)
    • php+mysql数据库批量根据条件快速更新、连表更新sql实现(0个评论)
    • 近期文章
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2017-06
    • 2017-08
    • 2017-09
    • 2017-10
    • 2017-11
    • 2018-01
    • 2018-05
    • 2018-10
    • 2018-11
    • 2020-02
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-08
    • 2020-09
    • 2021-02
    • 2021-04
    • 2021-07
    • 2021-08
    • 2021-11
    • 2021-12
    • 2022-02
    • 2022-03
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-07
    • 2023-08
    • 2023-10
    • 2023-11
    • 2023-12
    • 2024-01
    • 2024-03
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客