MySQL SELECT 聯集查詢

來源:互聯網
上載者:User

標籤:合并   color   ble   ber   sql   實現   desc   查詢   union   

這裡主要分享一下聯集查詢的用法。

MySQL使用UNIONUNION ALL實現資料的聯集查詢。

假設有以下兩個表:

mysql> select * from staff;+----+----------+-------+| id | name     | slary |+----+----------+-------+|  1 | guoding  |  3200 ||  2 | dingtao  |  2800 ||  3 | haofugui |  3500 ||  4 | guoming  |  4000 ||  5 | haotian  |  2900 ||  6 | fengfei  |  3200 ||  7 | guoting  |  2600 |+----+----------+-------+7 rows in set (0.00 sec)mysql> select * from staff_1;+----+----------+-------+| id | name     | slary |+----+----------+-------+|  1 | guoding  |  3200 ||  2 | liding   |  2700 ||  3 | haofugui |  3500 ||  4 | xiaoli   |  3600 ||  5 | yazhi    |  3200 ||  6 | yuanfei  |  3200 ||  7 | guoting  |  3500 |+----+----------+-------+7 rows in set (0.00 sec)

 mysql> select * from staff_2;
 +----+-------+----------+
 | id | slary | name |
 +----+-------+----------+
 | 1 | 3200 | guoding |
 | 2 | 2700 | liding |
 | 3 | 3500 | haofugui |
 | 4 | 3600 | xiaoli |
 | 5 | 3200 | yazhi |
 | 6 | 3200 | yuanfei |
 | 7 | 3500 | guoting |
 +----+-------+----------+
 7 rows in set (0.00 sec)

 

 mysql> select * from staff_3;
 +----------+-------+
 | name | slary |
 +----------+-------+
 | guoding | 3200 |
 | liding | 2700 |
 | haofugui | 3500 |
 | xiaoli | 3600 |
 +----------+-------+
 4 rows in set (0.00 sec)

 

1. UNION

語句:SELECT column_name FROM table1 UNION SELECT column_name FROM table2

說明:1)用於合并兩個或多個 SELECT 語句的結果集,並消去表中任何重複行;
             2)UNION 內部的 SELECT 語句必須擁有相同數量的列,列也必須擁有相似的資料類型;
             3)同時,每條 SELECT 語句中的列的順序必須相同;

             4)如果子句中有order by,limit等,需用括弧()包起來,推薦放到所有子句之後,即對最終合并的結果來排序或篩選。

樣本:

 
mysql> select * from staff union select * from staff_1;+----+----------+-------+| id | name     | slary |+----+----------+-------+|  1 | guoding  |  3200 ||  2 | dingtao  |  2800 ||  3 | haofugui |  3500 ||  4 | guoming  |  4000 ||  5 | haotian  |  2900 ||  6 | fengfei  |  3200 ||  7 | guoting  |  2600 ||  2 | liding   |  2700 ||  4 | xiaoli   |  3600 ||  5 | yazhi    |  3200 ||  6 | yuanfei  |  3200 ||  7 | guoting  |  3500 |+----+----------+-------+12 rows in set (0.00 sec)mysql> select * from staff union select * from staff_2;+----+----------------------+----------------------+| id | name                 | slary                |+----+----------------------+----------------------+|  1 | guoding              | 3200                 ||  2 | dingtao              | 2800                 ||  3 | haofugui             | 3500                 ||  4 | guoming              | 4000                 ||  5 | haotian              | 2900                 ||  6 | fengfei              | 3200                 ||  7 | guoting              | 2600                 ||  1 | 3200                 | guoding              ||  2 | 2700                 | liding               ||  3 | 3500                 | haofugui             ||  4 | 3600                 | xiaoli               ||  5 | 3200                 | yazhi                ||  6 | 3200                 | yuanfei              ||  7 | 3500                 | guoting              |+----+----------------------+----------------------+14 rows in set (0.00 sec)mysql> select * from staff union select name from staff_1;ERROR 1222 (21000): The used SELECT statements have a different number of columnsmysql> select * from staff union select * from staff_3;ERROR 1222 (21000): The used SELECT statements have a different number of columns
mysql> (select * from staff order by id asc) union (select * from staff_1 order by slary desc);+----+----------+-------+| id | name     | slary |+----+----------+-------+|  1 | guoding  |  3200 ||  2 | dingtao  |  2800 ||  3 | haofugui |  3500 ||  4 | guoming  |  4000 ||  5 | haotian  |  2900 ||  6 | fengfei  |  3200 ||  7 | guoting  |  2600 ||  2 | liding   |  2700 ||  4 | xiaoli   |  3600 ||  5 | yazhi    |  3200 ||  6 | yuanfei  |  3200 ||  7 | guoting  |  3500 |+----+----------+-------+12 rows in set (0.00 sec)

 

2. UNION ALL

作用及規則與UNION相同,區別是,UNION ALL不消除重複行

樣本:

mysql> select * from staff union select * from staff_1;+----+----------+-------+| id | name     | slary |+----+----------+-------+|  1 | guoding  |  3200 ||  2 | dingtao  |  2800 ||  3 | haofugui |  3500 ||  4 | guoming  |  4000 ||  5 | haotian  |  2900 ||  6 | fengfei  |  3200 ||  7 | guoting  |  2600 ||  2 | liding   |  2700 ||  4 | xiaoli   |  3600 ||  5 | yazhi    |  3200 ||  6 | yuanfei  |  3200 ||  7 | guoting  |  3500 |+----+----------+-------+12 rows in set (0.00 sec)mysql> select * from staff union all select * from staff_1;+----+----------+-------+| id | name     | slary |+----+----------+-------+|  1 | guoding  |  3200 ||  2 | dingtao  |  2800 ||  3 | haofugui |  3500 ||  4 | guoming  |  4000 ||  5 | haotian  |  2900 ||  6 | fengfei  |  3200 ||  7 | guoting  |  2600 ||  1 | guoding  |  3200 ||  2 | liding   |  2700 ||  3 | haofugui |  3500 ||  4 | xiaoli   |  3600 ||  5 | yazhi    |  3200 ||  6 | yuanfei  |  3200 ||  7 | guoting  |  3500 |+----+----------+-------+14 rows in set (0.00 sec)

 

MySQL SELECT 聯集查詢

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.