Oracle學習筆記(二)----------union

來源:互聯網
上載者:User

標籤:style   blog   color   io   資料   sp   div   on   log   

趁熱再整理一個關於資料庫(Oracle)的關鍵字的用法

 

union關鍵字

union字面意思就是“聯合”,通過該關鍵字可以將兩個sql語句合并起來一起查詢,以達到想要的查詢結果。

舉個例子

 

設有表如下:

學生表:

+----+------+------+---------+| id | name | age  | classid |+----+------+------+---------+|  1 | 關羽  |  35  |  7班    ||  2 | 張飛  |  33  |  5班    ||  3 | 趙雲  |  31  |  3班    ||  4 | 馬超  |  28  |  2班    ||  5 | 黃忠  |  60  |  8班    |+----+------+------+---------+

 

執行以下sql語句:

-- 通過union可以將兩個sql語句合并起來select * from studentwhere student.age >= 35unionselect * from studentwhere student.age < 30;

查詢結果:

+----+-------+-------+---------+| id | name  | age   | classid |+----+-------+-------+---------+|  1 |  關羽  |   35  |  7班    ||  5 |  黃忠  |   60  |  8班    ||  4 |  馬超  |   28  |  2班    |+----+--------+------+---------+

通過union關鍵字將兩條sql語句合并一起查詢,查詢到結果一併顯示出來。

 

再看另一條sql語句:

-- 通過union可以將兩個sql語句合并起來select * from studentwhere student.age >= 30unionselect * from studentwhere student.age < 35;

執行結果如下:

+----+------+------+---------+| id | name | age  | classid |+----+------+------+---------+|  1 | 關羽  |  35  |  7班    ||  2 | 張飛  |  33  |  5班    ||  3 | 趙雲  |  31  |  3班    ||  5 | 黃忠  |  60  |  8班    ||  4 | 馬超  |  28  |  2班    |+----+------+------+---------+

通過該sql語句查詢到所有記錄,可以看到union關鍵字相當於將兩個where字句的條件用或(||、or)關聯起來。注意union不會顯示重複的記錄。

 

與union關鍵字類似作用的就是union all關鍵字,兩者的區別是union all關鍵字查詢出來不會合并重複的記錄,union all會顯示查到的所有記錄,相同記錄可重複出現。

如上面的sql語句將關鍵字union換成union all,執行結果將會不一樣

sql語句:

-- 通過union可以將兩個sql語句合并起來select * from studentwhere student.age >= 30union allselect * from studentwhere student.age < 35;

union all執行結果:

+----+------+------+---------+| id | name | age  | classid |+----+------+------+---------+|  1 | 關羽  |  35  |  7班    ||  2 | 張飛  |  33  |  5班    ||  3 | 趙雲  |  31  |  3班    ||  5 | 黃忠  |  60  |  8班    ||  2 | 張飛  |  33  |  5班    ||  3 | 趙雲  |  31  |  3班    ||  4 | 馬超  |  28  |  2班    |+----+------+------+---------+

可以看到,union all也將兩條sql語句合并起來了,不同的是,每條sql查詢出來的結果都會直接顯示出來(前4條記錄是第一個sql,後三條是第二個sql),它並沒有將重複記錄合并起來。

必須要注意的是,union和union all查詢中,兩個select語句的欄位類型要匹配,欄位個數也要一致。

union和union all可以不局限於同一張表,兩個sql語句可以分別對應不同的表。

 

例如,還設有另一張表如下:

+------------+--------------+------+---------------+| teacher_id | teacher_name | age  | teach_classid |+------------+--------------+------+---------------+|    a       |    張三       |  50  |    7班        ||    b       |    李四       |  41  |    5班        ||    c       |    王五       |  38  |    3班        |+------------+--------------+------+---------------+

執行以下sql語句:

select * from studentwhere student.age >= 30unionselect * from teacherwhere teacher.age < 50;

執行結果如下:

+----+------+------+---------+| id | name | age  | classid |+----+------+------+---------+| 1  | 關羽  |  35  |   7班   || 2  | 張飛  |  33  |   5班   || 3  | 趙雲  |  31  |   3班   || 5  | 黃忠  |  60  |   8班   || b  | 李四  |  41  |   5班   || c  | 王五  |  38  |   3班   |+----+------+------+---------+

上面這個例子通過union關鍵字將兩條分別查詢不同表的sql語句合并起來了,注意合并之後的欄位名稱以第一條sql語句的欄位名稱來命名。

union all同樣道理,只不過不會合并重複記錄。

 

 

 

Oracle學習筆記(二)----------union

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.