Oracle union多表查詢


union就是把兩個結果集合並起來,被合並的兩個結果集的字段數量要相同,數據類型要相似(兼容)。

union在合並兩個結果集的時候,會自動去除重復的數據。

union all在合並兩個結果集的時候,只是簡單的將兩個結果集中的數據進行連接,不會去除重復的數據。

我通過一些示例來向大家介紹子查詢的常用方法。

一、生成測試數據

1、創建超女基本信息歷史表(T_GIRL_HIS)

create table T_GIRL_HIS
(
  id        char(4)         not null,   -- 編號
  name      varchar2(10)    not null,   -- 姓名
  yz        varchar2(10)        null,   -- 顏值
  sc        varchar2(10)        null,   -- 身材
  weight    number(4,1)     not null,   -- 體重
  height    number(3)       not null,   -- 身高
  birthday  date            not null,   -- 出生時間
  memo      varchar2(1000)      null,    -- 備注
  primary key (id)
);
insert into T_GIRL_HIS(id,name,yz,birthday,sc,weight,height,memo)
  values('0101','西施','漂亮',to_date('2000-01-01 01:12:35','yyyy-mm-dd hh24:mi:ss'),
         '火辣',48.5,170,'這是一個非常漂亮姑娘,老公是夫差,男朋友是范蠡。');
insert into T_GIRL_HIS(id,name,yz,birthday,sc,weight,height,memo)
  values('0102','貂禪','漂亮',to_date('1997-08-02 12:20:38','yyyy-mm-dd hh24:mi:ss'),
         '苗條',45.2,168,'王允真不是男人,干不過董卓就把美人往火坑里推,千古罪人啊。');
insert into T_GIRL_HIS(id,name,yz,birthday,sc,weight,height,memo)
  values('0103','妲已','漂亮',to_date('1998-03-03 10:50:33','yyyy-mm-dd hh24:mi:ss'),
         '火辣',53.6,172,'如果商真的因我而亡,您們男人做什么去了?');
insert into T_GIRL_HIS(id,name,yz,birthday,sc,weight,height,memo)
  values('0104','芙蓉姐姐','豬扒',to_date('1980-05-05 10:11:55','yyyy-mm-dd hh24:mi:ss'),
         '膘肥體壯',85.8,166,'如果不努力學習技術,將來就會娶個芙蓉姐姐,哼哼。');

2、創建超女基本信息表(T_GIRL)

create table T_GIRL
(
  id        char(4)         not null,   -- 編號
  name      varchar2(10)    not null,   -- 姓名
  yz        varchar2(10)        null,   -- 顏值
  sc        varchar2(10)        null,   -- 身材
  weight    number(4,1)     not null,   -- 體重
  height    number(3)       not null,   -- 身高
  birthday  date            not null,   -- 出生時間
  memo      varchar2(1000)      null,    -- 備注
  primary key(id)
);
insert into T_GIRL(id,name,yz,birthday,sc,weight,height,memo)
  values('0103','妲已','漂亮',to_date('1998-03-03 10:50:33','yyyy-mm-dd hh24:mi:ss'),
         '火辣',53.6,172,'如果商真的因我而亡,您們男人做什么去了?');
insert into T_GIRL(id,name,yz,birthday,sc,weight,height,memo)
  values('0104','芙蓉姐姐','豬扒',to_date('1980-05-05 10:11:55','yyyy-mm-dd hh24:mi:ss'),
         '膘肥體壯',85.8,166,'如果不努力學習技術,將來就會娶個芙蓉姐姐,哼哼。');
insert into T_GIRL(id,name,yz,birthday,sc,weight,height,memo)
  values('0105','神密貓女',null,to_date('1989-12-08 12:10:35','yyyy-mm-dd hh24:mi:ss'),
         null,48.5,171,'不知道是什么人,她臉上有一個%符號,很神密。');

3、測試數據說明

在這里插入圖片描述

在這里插入圖片描述

超女基本信息歷史表(T_GIRL_HIS)中有4條記錄,超女基本信息表(T_GIRL)中有3條記錄,兩個表中有相交的記錄('0103'、'0104'),在圖中已用方框圈了出來。

二、union示例

1、union(去重復記錄的聯合)

union在合並兩個結果集的時候,會自動去除重復的數據。T_GIRL和T_GIRL_HIS用union聯合后的結果集有5條記錄。

select id,name,yz,sc,weight,height,birthday,memo from T_GIRL
union 
select id,name,yz,sc,weight,height,birthday,memo from T_GIRL_HIS;

在這里插入圖片描述

2、union all(不去復記錄的重聯合)

union all在合並兩個結果集的時候,只是簡單的將兩個結果集中的數據進行連接,不會去除重復的數據。T_GIRL和T_GIRL_HIS用union all聯合后的結果集有7條記錄。

select id,name,yz,sc,weight,height,birthday,memo from T_GIRL
union all 
select id,name,yz,sc,weight,height,birthday,memo from T_GIRL_HIS;

在這里插入圖片描述

3、從聯合后的結果集中查詢

select distinct id,name,yz,sc,weight,height,birthday,memo from 
  (
    select id,name,yz,sc,weight,height,birthday,memo from T_GIRL
    union all
    select id,name,yz,sc,weight,height,birthday,memo from T_GIRL_HIS
  ) order by id;

在這里插入圖片描述

以上SQL的功能相當於union去重復記錄的聯合查詢。

三、應用經驗

union在進行結果集聯合后會篩選掉重復的記錄,所以在表聯合后會對所產生的結果集進行排序,刪除重復的記錄后再返回結果。

而union all只是簡單的將兩個結果集合並后就返回,如果返回的兩個結果集中有重復的數據,那么返回的結果集就會包含重復的數據。

從效率上講,union all要比union快很多,所以,如果可以確定合並的兩個結果集中不會包含重復的數據,就應該使用union all。

四、版權聲明

C語言技術網原創文章,轉載請說明文章的來源、作者和原文的鏈接。
來源:C語言技術網(www.freecplus.net)
作者:碼農有道

如果文章有錯別字,或者內容有錯誤,或其他的建議和意見,請您留言指正,非常感謝!!!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM