专业IT网络知识平台,分享IT百科知识、生活百科知识解答!

易企推科技
易企推科技

unionall和union的区别用法

来源:小易整编  作者:小易  发布时间:2022-12-29 02:10
摘要:unionall和union的区别用法union:对两个结果集进行并集操作,不包括重复行,相当于distinct,同时进行默认规则的排序unionall:对两个结果集进行并集操作,包括重复行,即所有的结果全部显示,不管是不是重复下面我们...

unionall和union的区别用法

union: 对两个结果集进行并集操作, 不包括重复行,相当于distinct, 同时进行默认规则的排序;

union all: 对两个结果集进行并集操作, 包括重复行, 即所有的结果全部显示, 不管是不是重复;

下面我们举一个简单的例子来证明上面的结论:

1. 准备数据:

drop table student;

create table student

(

id int primary key,

name nvarchar2(50) not null,

score number not null

);

insert into student values(1,'Aaron',78);

insert into student values(2,'Bill',76);

insert into student values(3,'Cindy',89);

insert into student values(4,'Damon',90);

insert into student values(5,'Ella',73);

insert into student values(6,'Frado',61);

insert into student values(7,'Gill',99);

insert into student values(8,'Hellen',56);

insert into student values(9,'Ivan',93);

insert into student values(10,'Jay',90);

commit;

2. 比较不同点

查询比较①

-- union all

select * from student where id < 4

union all

select * from student where id > 2 and id < 6

-- union

select * from student where id < 4

union

select * from student where id > 2 and id < 6

union all 查询结果:

unionall和union的区别用法

union 查询结果:

unionall和union的区别用法

通过比较不难看出, union all不会去掉重复记录, 而union去掉了重复的记录.

查询比较②

-- union all

select * from student where id > 2 and id < 6

union all

select * from student where id < 4

-- union

select * from student where id > 2 and id < 6

union

select * from student where id < 4

union all 查询结果:

unionall和union的区别用法

union 查询结果:

unionall和union的区别用法

通过比较不难看出, union all会按照关联的次序组织数据, 而union会依据一定的规则进行排序.

那么这个规则是什么呢? 我们通过下面的查询得出规律:

-- union

select score,id,name from student where id > 2 and id < 6

union

select score,id,name from student where id < 4

结论: 按照字段出现的顺序进行排序, 之前的查询相当于order by id, name, score, 刚刚的查询相当于order by score, id, name.

unionall和union的区别用法

[ 总结 ]

1. 因为union all仅仅是简单的合并查询结果, 并不会做去重操作, 也不会排序, 所以union all效率要比union高.

所以在能够确定没有重复记录的情况下, 尽量使用union all.

2. 通常如果表有多个索引列时, 用union替换where子句中的or会起到较好的效果, 索引列使用or会造成全表扫描.

注意: 以上规则只针对多个索引列有效, 假如有column没有被索引, 那还是用or吧.

例如: 还是使用上面的例子, 假定name和score上建有索引.

-- 高效

select id, name, score from student where name like '%y%'

union

select id, name, score from student where score between 80 and 90

-- 低效

select id, name, score from student where name like '%y%' or score between 80 and 90


本文地址:IT百科频道 https://www.hkm168.com/tags/886463.html,易企推百科一个免费的知识分享平台,本站部分文章来网络分享,本着互联网分享的精神,如有涉及到您的权益,请联系我们删除,谢谢!


IT百科
小编:小易整编
相关文章相关阅读
  • uri和url区别和关联

    uri和url区别和关联

    uri和url区别和关联URI,是uniformresourceidentifier,统一资源标识符,用来唯一的标识一个资源。Web上可用的每种资源如HTML文档、图像、视频片段、程序等都是一个来URI来定位的URI一般由三部组成:①访...

  • 什么是Linux系统中nc命令?nc命令的用法详解

    什么是Linux系统中nc命令?nc命令的用法详解

    这篇文章主要介绍了linux系统中nc命令的基本用法,nc命令非常之强大,这里先简单介绍它用来作端口扫描以及文件传输等的基础使用。功能说明:功能强大的网络工具,在网络工具中有“瑞士军刀”美誉,其有Windows和Linux的版本。因为它短小...

  • 数据库的substr函数用法是什么

    数据库的substr函数用法是什么

    数据库的substr函数用法:1、【substr(str,pos,len)】从pos开始的位置,截取len个字符;2、【substr(str,pos)】pos开始的位置,一直截取到最后。数据库的substr函数用法:1、SUBSTR(st...

  • 微信公众号认证和不认证有哪些区别

    微信公众号认证和不认证有哪些区别

    微信公众号认证和不认证的区别在认证标识、功能权限、推送频率、接口权限和用户信任度等方面。详细介绍:1、认证标识,认证公众号会获得官方颁发的认证标识,即蓝色v标志,这个标志可以增加公众号的可信度和权威性,让用户更容易辨别真实的官方公众号;2、...

  • c语言中pow函数的用法是什么?

    c语言中pow函数的用法是什么?

    在c语言中pow()函数是用来求x的y次幂。x、y及函数值都是double型,其语法为“doublepow(doublex,doubley)”;其中参数“doublex”表示底数;参数“doubley”表示指数。pow()函数...

  • c语言中fun用法详解

    c语言中fun用法详解

    c语言中fun用法详解fun函数是自定义的C/C++语言函数,函数功能多样。该函数名为“函数”英文function的简写,一般在示例和试题中使用,通常在主函数中被调用。C/C++语言中,fun函数通常被主函数所调用。它是指用fun来定义一个...

  • JSF和JavaScript有什么区别

    JSF和JavaScript有什么区别

    区别:jsf是一种用于构建web应用程序的java框架,提供了一种以组件为中心来开发javaweb用户界面的方法,从而简化了开发。而javascript是一种解释型的脚本语言,被广泛用于web应用开发,常用来为网页添加各式各样的动态功能。...

  • matlab中zeros函数用法

    matlab中zeros函数用法

    matlab中zeros函数是用于返回一个double类零矩阵,其用法是:1、在命令行窗口中输入“b=zeros(5)”,按回车键可生成一个“5*5”的零矩阵;2、在命令行窗口中输入“b=zeros(3,4)”,并按回车键即可。zeros函...

  • 周排行
  • 月排行
  • 年排行

精彩推荐