ITPub博客

首页 > 数据库 > Oracle > oracle中UNION的用法说明 分类:oracle栏目

oracle中UNION的用法说明 分类:oracle栏目

Oracle 作者:xqch1983 时间:2008-01-11 11:02:43 0 删除 编辑
 

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

union  会排除重复的记录  
union  all  不会排除重复的记录  
union all 比union 速度快很多,建议在相同的情况下用 union all

注意:union用法中,两个select语句的字段类型匹配,而且字段个数要相同。

例:

1、建两表并向其中插入示例数据:

create  table  apps.TABLE_A
       (A  VARCHAR2(10),
       B  VARCHAR2(10));
   
insert into TABLE_A(A,B) values ('1','A');
insert into TABLE_A(A,B) values ('2','A');
insert into TABLE_A(A,B) values ('3','B');
insert into TABLE_A(A,B) values ('4','B');

select * from TABLE_A

create  table  apps.TABLE_B 
       (A  VARCHAR2(10), 
       B  VARCHAR2(10));
   
insert into TABLE_B (A,B) values ('2','A'); 
insert into TABLE_B (A,B) values('3','A');
insert into TABLE_B (A,B) values('4','A');

select * from TABLE_B;

2、要求:查询两张表中B相等的且B='A'的记录。

3、分析:结果应该是五条记录。

4、测试:
     a、select * from TABLE_A A,TABLE_B B where A.B=B.B;
           oracle中UNION的用法说明 <wbr>分类:oracle栏目

      b、select  *  from  TABLE_A  where  TABLE_A.b='A' union all select  *  from  TABLE_B  where  TABLE_B.B='A';
           oracle中UNION的用法说明 <wbr>分类:oracle栏目

     c、select  *  from  TABLE_A  where  TABLE_A.b='A' union select  *  from  TABLE_B  where  TABLE_B.B='A';
           oracle中UNION的用法说明 <wbr>分类:oracle栏目

5、结果:union all 正确

<!-- 正文结束 -->

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/22181866/viewspace-1115864/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: 没有了~
下一篇: 没有了~
请登录后发表评论 登录
全部评论

注册时间:2009-07-24