oracle 将多个查询结果合并

       首先要区分一个概况,多个查询结果分两种,一种是一条sql查询多个结果,还有一种是多条sql语句查询出多个结果。

       先看第一个,一条语句查询出多个结果,将多个结果合并成一条记录:

       在oracle中有一个wm_concat()函数。

oracle wm_concat(column)函数使我们经常会使用到的,下面就教您如何使用oraclewm_concat(column)函数实现字段合并

如:

shopping:

-----------------------------------------

u_id       goods            num

------------------------------------------

1                苹果                2

2                 梨子               5

1                 西瓜               4

3                 葡萄               1

3                香蕉                1

1               橘子                 3

=======================

想要的结果为:

--------------------------------

u_id          goods_sum

____________________

1              苹果,西瓜,橘子

2              梨子

3              葡萄,香蕉

---------------------------------

select u_id, wmsys.wm_concat(goods) goods_sum   from shopping   group by u_id  

想要的结果2:

--------------------------------

u_id          goods_sum

____________________

1              苹果(2斤),西瓜(4斤),橘子(3斤)

2              梨子(5斤)

3              葡萄(1斤),香蕉(1斤)

---------------------------------

使用oracle wm_concat(column)函数实现:

select u_id, wmsys.wm_concat(goods || '(' || num || '斤)' ) goods_sum   from shopping   group by u_id。

以上案例借鉴于:https://www.cnblogs.com/qianyuliang/p/6649983.html

接下来看将多个语句的查询结果合并:

oracle 中有union() union all()函数:

下面我举个栗子:

对这个概念理解可能会有些模糊。我们通过实例来讲解,首先创建一个表Student,插入一些相应的测试数据。sql语句如下:

复制代码

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;

复制代码

3、测试 union 命令的结果集,sql语句如下:

复制代码

select *

from student

where id < 4

union

select *

from student

where id > 2 and id < 6

复制代码

4、结果将是:

      1    Aaron    78

      2    Bill    76

      3    Cindy    89

      4    Damon    90

      5    Ella    73

5、如果将union换成union all连接两个结果集,则返回结果是:

             1    Aaron    78

    2    Bill    76

    3    Cindy    89

    3    Cindy    89

    4    Damon    90

    5    Ella    73

         小结: 可以看到,Union和Union All的区别之一在于对重复结果的处理。接下来我们将两个子查询的顺序调整一下,改为

--Union

复制代码

select *

from student

where id > 2 and id < 6

union

select *

from student

where id < 4

复制代码

看看执行结果是否和你期望的一致?

--Union All

复制代码

select *

from student

where id > 2 and id < 6

union all

select *

from student

where id < 4

复制代码

    那么这个呢?答案是一模一样的~

6、据此我们可知,union和union all 区别在于在于对排序的处理。Union All将按照关联的次序组织数据,而Union将进行依据一定规则进行排序。那么这个排序规则是?我们换个查询方式看看:

复制代码

select score,id,name

from student

where id > 2 and id < 6

union

select score,id,name

from student

where id < 4

复制代码

结果如下:

    73    5    Ella

    76    2    Bill

    78    1    Aaron

    89    3    Cindy

    90    4    Damon

和我们预料的一致:将会按照字段的顺序进行排序。之前我们的查询是基于id,name,score的字段顺序,那么结果集将按照id优先进行排序;而现在新的字段顺序也改变了查询结果的排序。并且,是按照给定字段a,b,c...的顺序进行的order by。即结果是order by a,b,c...........的。我们看下一个查询:

复制代码

select score,id,name

from student

where id > 2

union

select score,id,name

from student

where id < 4

复制代码

结果如下:

    56    8    Hellen

    61    6    Frado

    73    5    Ella

    76    2    Bill

    78    1    Aaron

    89    3    Cindy

    90    4    Damon

    90    10    Jay

    93    9    Ivan

    99    7    Gill

        可以看到,对于score相同的记录,将按照下一个字段id进行排序。如果我们想自行控制排序,是不是用order by指定就可以了呢?答案是肯定的,不过在写法上有需要注意的地方:

复制代码

select score,id,name

from student

where id > 2 and id < 7

union

select score,id,name

from student

where id < 4

union

select score,id,name

from student

where id > 8

order by id desc

复制代码

     order by子句必须写在最后一个结果集里,并且其排序规则将改变操作后的排序结果。对于Union、Union All都有效。

7、注意:

    1,Union可以对字段名不同但数据类型相同的结果集进行合并;

    2,如果字段名不同的结果集进行Union,那么对此字段的Order by子句将失效。

    这里一起总结一下:

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

    Union All,对两个结果集进行并集操作,包括重复行,不进行排序;2016-12-23

    可以在最后一个结果集中指定Order by子句改变排序方式

上面这个栗子我是从https://www.cnblogs.com/yuanchaoyong/p/6213885.html获取到的。

谢谢两位博主的分享。

猜你喜欢

转载自blog.csdn.net/qq_34756209/article/details/82491317