广告合作
  • 今日头条

    今日头条

  • 百度一下

    百度一下,你就知道

  • 新浪网

    新浪网 - 提供新闻线索,重大新闻爆料

  • 搜狐

    搜狐

  • 豆瓣

    豆瓣

  • 百度贴吧

    百度贴吧——全球领先的中文社区

  • 首页 尚未审核订阅工具 订阅

    oracle中union和union all的区别和使用

    来源:网络收集  点击:  时间:2024-03-11
    【导读】:
    在使用sql语句时,我们会经常使用到union和union all两个语法,这两个的用法其实是不同的,今天我们来看看他们有什么区别工具/原料moreoracle数据库方法/步骤1/5分步阅读

    步骤一:首先手下连个关键字的区别

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

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

    2/5

    步骤二:通过实验来看看,首先创建数据

    drop table student2;

    create table student2

    (

    id int primary key,

    name varchar2(50) not null,

    age number not null

    );

    insert into student2 values(1,张三,23);

    insert into student2 values(2,李四,21);

    insert into student2 values(3,王二,20);

    insert into student2 values(4,郭经,20);

    insert into student2 values(5,王伟,23);

    insert into student2 values(6,Frado,24);

    insert into student2 values(7,伍梅,23);

    insert into student2 values(8,张伦,23);

    insert into student2 values(9,郭飞,21);

    insert into student2 values(10,刘鹏飞,22);

    commit;

    3/5

    步骤三:区别1

    -- union

    select * from student2 where id 4

    union

    select * from student2 where id 2 and id 6

    看到结果中ID=3的只有一条

    -- union all

    select * from student2 where id 4

    union all

    select * from student2 where id 2 and id 6

    结果中ID=3的结果有两个union和union all的区别在于union all取结果的交集之后不会进行去重

    4/5

    步骤四:区别二

    -- union all

    select * from student2 where id 2 and id 6

    union all

    select * from student2 where id 4

    -- union

    select * from student2 where id 2 and id 6

    union

    select * from student2 where id 4

    从结果中可以看到union还对获取的结果进行排序操作

    5/5

    步骤五:总结:union all只是合并查询结果,并不会进行去重和排序操作,在没有去重的前提下,使用union all的执行效率要比union高

    注意事项

    喜欢记得点赞,你的认可是我创作的动力

    ORACLEUNIONSQL
    本文关键词:

    版权声明:

    1、本文系转载,版权归原作者所有,旨在传递信息,不代表看本站的观点和立场。

    2、本站仅提供信息发布平台,不承担相关法律责任。

    3、若侵犯您的版权或隐私,请联系本站管理员删除。

    4、文章链接:http://www.1haoku.cn/art_319816.html

    ©2019-2020 http://www.1haoku.cn/ 国ICP备20009186号06-01 16:27:37  耗时:0.022