`
满楼都是我的人
  • 浏览: 11780 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

oracle中如何区分union与union all的使用

阅读更多
oracle中如何区分union与union all的使用

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

union all 对两个结果集进行并集操作,包括重复行,不进行排序


union all 要比union快很多,所以,如果可以确认合并的两个结果集中不包含重复的数据的话,那么就使用union all,如下:

尽量使用union all,因为union需要进行排序,去除重复记录,效率低
union
如果表有多个索引列的时候,用union 替换 where 中的or 效率会提高不少。索引列使用or会造成全表扫描。如果有column 没有使用索引,就得记得or了。

    select date from store_information
    union
    select date from internet_sales

注意:union用法中,两个select语句的字段类型匹配,而且字段个数要相同,如上面的例子,在实际的软件开发过程,www.3ppt.com会遇到更复杂的情况,具体请看下面的例子

    select '1' as type,fl_id,fl_code,fl_cname,flda.fl_parentid from flda
    where zt_id=2006030002
    union
    select '2' as type,xm_id,xm_code ,xm_cname ,fl_id from xmda
    where exists (select * from (select fl_id from flda where zt_id=2006030002 ) a where xmda.fl_id=a.fl_id)
    order by type,fl_parentid ,fl_id

这个句子的意思是将两个sql语句union查询出来,查询的条件就是看xmda表中的fl_id是否和主表flda里的fl_id值相匹配,(也就是存在).



union all 详细实例

union 指令的目的是将两个 sql 语句的结果合并起来,可以查看你要的查询结果.

例如:



    sql>   select   *   from   a;

    id                   name
    ----------   ----------
    1                     aa
    2                     bb
    3                     cc
    6                     dd
    7                     ee

    sql>   select   *   from   b;

    id                   addr
    ----------   ----------
    1                     aa
    2                     bb
    3                     cc
    4                     dd
    5                     ee

    sql>   select   *   from   a
        2     union   all
        3     select   *   from   b;

    id                   name
    ----------   ----------
    1                     aa
    2                     bb
    3                     cc
    6                     dd
    7                     ee
    1                     aa
    2                     bb
    3                     cc
    4                     dd
    5                     ee

    已选择10行。

    sql>   select   *   from   a
        2     union 
        3     select   *   from   b;

    id                   name
    ----------   ----------
    1                     aa
    2                     bb
    3                     cc
    4                     dd
    5                     ee
    6                     dd
    7                     ee

已选择7行。

sql>

注意:union用法中,两个select语句的字段类型匹配,而且字段个数要相同,如上面的例子,在实际的软件开发过程,会遇到更复杂的情况
分享到:
评论

相关推荐

    Oracle中的Union、Union_All、Intersect、Minus

    Oracle中的Union、Union_All、Intersect、Minus

    oracle集合union、union all、intersect、minus

    oracle集合union、union all、intersect、minus

    union all与order by用法

    union all与order by用法,并详细举例,oracle pl/sql

    Oracle中Union与Union All的区别(适用多个数据库)

    Union 与 Union ALL 的作用都是合并 SELECT 的查询结果集,那么它们有什么不同呢? Union 将查询到的结果集合并后进行重查,将其中相同的行去除。缺点:效率低; 而Union ALL 则只是合并查询的结果集,并不重新查询...

    27.Oracle union多表查询1

    3、测试数据说明超女基本信息历史表(T_GIRL_HIS)中有4条记录,超女基本信息表(T_GIRL)中有3条记录,两个表中有相交的记录('0103'、'010

    浅谈Oracle数据库性能的优化

    提出了一种优化Oracle 数据库的方法...Oracle 中SQL 语句的执行过程可分为解析(Parse)、执行(Execute)和提取结果(Fetch)三步,此方法就是通过对SQL 语句在Oracle 数据库中优化执行的三个过程来提高Oracle 数据库的性能。

    Oracle SQL最佳实践

    1.用EXISTS代替DISTINCT,消除sort operation  2.如果在GROUP BY中过滤数据,在WHERE从句中指定条件比在HAVING从句中有更好的...因此如果应用能够处理重复,或者确信没有重复记录,那么考虑使用UNION ALL代替UNION

    Oracle-[WITH & CONNECT

    union all select '1' as pid, '2' as id, '2' as name from dual union all select '1' as pid, '3' as id, '3' as name from dual union all select '2' as pid, '5' as id, '5' as name from dual union ...

    oracle学习日志总结

    尽量使用“>=”,不要使用“>”,用EXISTS代替IN(外表数据小情况),用大于或小于代替不等于,用右模糊查询(LIKE ‘…%’)代替模糊查询,用UNION ALL代替UNION,union代替or,trancate代替delete等. 7. count(1)比...

    Oracle 多行记录合并_连接_聚合字符串的几种方法_oracle_脚本之家1

    1.被集合字段范围小且固定型 灵活性 性能 难度 2.固定表固定字段函数法 灵活性 性能 难度 3.灵活表函数法 灵活性 性能 难度 4.一条SQL法 灵活性

    oracle的sql优化

     在不需要考虑重复记录合并时候用Union All来代替Union  使用显性游标而不使用隐性游标,特别是大数据量情况下隐性游标对性能影响很大  是否使用函数的问题  用直接的表关联来代替Exist.用Exist或Not Exists来...

    Oracle的列转行问题

    Oracle中使用语句将行数据转换称不同的列表示,或者将不同的列数据写到同一列的不同行上的行列转换问题是一个非常传统的话题。 网络上流传了很多将行数据转换称列数据的方法和应用实例,一般通过decode或者case函数...

    oracle 数据库所有示例

    select iname,iage,ideptno from showinfo where ideptno in(10,20)union all select iname,iage,ideptno from showinfo where ideptno=10; --就好比 (20,30)减去30 只剩下20 了一样 结果A减去结果B select sname,...

    Oracle数据库Sql性能调优

    1.39 用UNION-ALL 替换UNION ( 如果有可能的话) 24 1.40 使用提示(HINTS) 25 1.41 用WHERE替代ORDER BY 25 1.42 避免改变索引列的类型. 26 1.43 需要当心的WHERE子句 27 1.44 连接多个扫描 28 1.45 CBO下使用更具...

    5分钟了解MySQL5.7中union all用法的黑科技

    union all在MySQL5.6下的表现 Part1:MySQL5.6.25 [root@HE1 ~]# MySQL -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: ...

    oracle sql of extracting table structure

    union all select distinct lower(all_c.table_name) table_name --,(case -- when column_id || to_char(column_id) -- when column_id (column_id) -- when column_id > 100 then 'H' || substrb(to_char...

    Oracle SQL高级编程(资深Oracle专家力作,OakTable团队推荐)--随书源代码

    4.2.1 UNION和UNION ALL 103 4.2.2 MINUS 106 4.2.3 INTERSECT 107 4.3 集合与空值 108 4.3.1 空值与非直观结果 108 4.3.2 集合运算中的空值行为 110 4.3.3 空值与GROUP BY和ORDER BY 112 4.3.4 空值与聚合...

    Oracle Database 11g初学者指南--详细书签版

    7.9.5 在OEM中使用RMAN 207 7.9.6 执行备份 209 7.9.7 还原和恢复 210 7.10 本章测验 213 第8章 高可用性:RAC、ASM和 Data Guard 215 8.1 高可用性定义 216 8.2 了解RAC 216 8.3 安装RAC 217 8.4 测试RAC ...

Global site tag (gtag.js) - Google Analytics