UNION 用于將多個 SELECT 語句的結(jié)果合并到結(jié)果集中犁跪。第一個 SELECT 語句的列名被用作返回結(jié)果的列列名炒瘸。在每個 SELECT 語句的相應(yīng)位置列出的選定列應(yīng)該有相同的數(shù)據(jù)類型牛哺。
測試數(shù)據(jù)和表結(jié)構(gòu)
mysql> desc books;
+-------------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+---------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(255) | NO | | NULL | |
| create_time | datetime | NO | | NULL | |
| is_hot | tinyint(3) unsigned | NO | | 0 | |
+-------------+---------------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
mysql> select * from books;
+----+---------------------------------------+---------------------+--------+
| id | name | create_time | is_hot |
+----+---------------------------------------+---------------------+--------+
| 1 | Head First Java | 2017-12-04 17:19:36 | 0 |
| 2 | Think in Java | 2017-12-02 17:19:36 | 0 |
| 3 | Clean Code | 2017-12-01 17:19:36 | 0 |
| 4 | The Ruby Programming | 2017-12-05 17:19:36 | 1 |
| 5 | JavaScript Definitive Guides | 2017-12-06 17:19:36 | 0 |
| 6 | Java 9 Data Structures and Algorithms | 2017-12-07 17:19:36 | 1 |
| 7 | The C Programming Language | 2017-12-08 17:19:36 | 0 |
| 8 | Pro Spring Boot | 2017-12-09 17:19:36 | 1 |
+----+---------------------------------------+---------------------+--------+
8 rows in set (0.00 sec)
組合查詢
where 和 union 語句在多數(shù)情況下可以實現(xiàn)相同的結(jié)果集讼渊。where 可以實現(xiàn)的語句棍潘,一定可以用 union 實現(xiàn)瓶籽。而 union 可以實現(xiàn)的語句匠童,where 卻不一定能做到。因為塑顺,union 是可以針對多張表進(jìn)行結(jié)果集的合并汤求。
查詢 id = 1
的記錄
mysql> select * from books where id = 1;
+----+-----------------+---------------------+--------+
| id | name | create_time | is_hot |
+----+-----------------+---------------------+--------+
| 1 | Head First Java | 2017-12-04 17:19:36 | 0 |
+----+-----------------+---------------------+--------+
1 row in set (0.00 sec)
查詢 is_hot = 1
的記錄
mysql> select * from books where is_hot = 1;
+----+---------------------------------------+---------------------+--------+
| id | name | create_time | is_hot |
+----+---------------------------------------+---------------------+--------+
| 4 | The Ruby Programming | 2017-12-05 17:19:36 | 1 |
| 6 | Java 9 Data Structures and Algorithms | 2017-12-07 17:19:36 | 1 |
| 8 | Pro Spring Boot | 2017-12-09 17:19:36 | 1 |
+----+---------------------------------------+---------------------+--------+
3 rows in set (0.00 sec)
使用 union
合并兩個查詢
mysql> select * from books where id = 1 union select * from books where is_hot = 1;
+----+---------------------------------------+---------------------+--------+
| id | name | create_time | is_hot |
+----+---------------------------------------+---------------------+--------+
| 1 | Head First Java | 2017-12-04 17:19:36 | 0 |
| 4 | The Ruby Programming | 2017-12-05 17:19:36 | 1 |
| 6 | Java 9 Data Structures and Algorithms | 2017-12-07 17:19:36 | 1 |
| 8 | Pro Spring Boot | 2017-12-09 17:19:36 | 1 |
+----+---------------------------------------+---------------------+--------+
4 rows in set (0.00 sec)
當(dāng)然,我們通過 select * from books where id = 1 or is_hot = 1
也可以做到严拒,但是 union 是不限于表的扬绪。也就是說,我們可以從多個表中查詢結(jié)果集出來糙俗,然后 merge 到第一個查詢語句查詢的結(jié)果集中(前提是字段數(shù)量和類型一致)勒奇。
重復(fù)項
默認(rèn)情況下,我們單單使用 union
語句巧骚,會自動幫我們?nèi)コ貜?fù)的項赊颠。但是,如果我們使用了 union all
語句劈彪,數(shù)據(jù)庫不會幫我們自動去除重復(fù)項竣蹦。
mysql> select * from books where id > 5 union select * from books where is_hot = 1;
+----+---------------------------------------+---------------------+--------+
| id | name | create_time | is_hot |
+----+---------------------------------------+---------------------+--------+
| 6 | Java 9 Data Structures and Algorithms | 2017-12-07 17:19:36 | 1 |
| 7 | The C Programming Language | 2017-12-08 17:19:36 | 0 |
| 8 | Pro Spring Boot | 2017-12-09 17:19:36 | 1 |
| 4 | The Ruby Programming | 2017-12-05 17:19:36 | 1 |
+----+---------------------------------------+---------------------+--------+
4 rows in set (0.00 sec)
mysql> select * from books where id > 5 union all select * from books where is_hot = 1;
+----+---------------------------------------+---------------------+--------+
| id | name | create_time | is_hot |
+----+---------------------------------------+---------------------+--------+
| 6 | Java 9 Data Structures and Algorithms | 2017-12-07 17:19:36 | 1 |
| 7 | The C Programming Language | 2017-12-08 17:19:36 | 0 |
| 8 | Pro Spring Boot | 2017-12-09 17:19:36 | 1 |
| 4 | The Ruby Programming | 2017-12-05 17:19:36 | 1 |
| 6 | Java 9 Data Structures and Algorithms | 2017-12-07 17:19:36 | 1 |
| 8 | Pro Spring Boot | 2017-12-09 17:19:36 | 1 |
+----+---------------------------------------+---------------------+--------+
6 rows in set (0.00 sec)
關(guān)于排序
union
中的排序是比較坑的,即使我們在部分結(jié)果集這種寫了 order by
語句沧奴。因為痘括,union
會在合并結(jié)果集之后自動的進(jìn)行排序。意味著在子結(jié)果集中的排序滔吠,排了也是白排纲菌。例如:
mysql> (select * from books where is_hot = 1 order by create_time desc)
-> union
-> (select * from books where is_hot = 0 order by create_time desc);
+----+---------------------------------------+---------------------+--------+
| id | name | create_time | is_hot |
+----+---------------------------------------+---------------------+--------+
| 4 | The Ruby Programming | 2017-12-05 17:19:36 | 1 |
| 6 | Java 9 Data Structures and Algorithms | 2017-12-07 17:19:36 | 1 |
| 8 | Pro Spring Boot | 2017-12-09 17:19:36 | 1 |
| 1 | Head First Java | 2017-12-04 17:19:36 | 0 |
| 2 | Think in Java | 2017-12-02 17:19:36 | 0 |
| 3 | Clean Code | 2017-12-01 17:19:36 | 0 |
| 5 | JavaScript Definitive Guides | 2017-12-06 17:19:36 | 0 |
| 7 | The C Programming Language | 2017-12-08 17:19:36 | 0 |
+----+---------------------------------------+---------------------+--------+
8 rows in set (0.00 sec)
從查詢的意圖來看,我們是想讓 is_hot = 1
和 is_hot = 0
的結(jié)果集各自按 create_time
字段進(jìn)行排序疮绷。但是結(jié)果出乎意外翰舌,并沒有按我們設(shè)定的順序進(jìn)行。
其實冬骚,子結(jié)果集是進(jìn)行了排序的椅贱,只不過 union 在合并結(jié)果集之后,會自動的對整個結(jié)果集進(jìn)行排序只冻。
假如我們現(xiàn)在有一個需求庇麦,is_hot = 1
的是熱門書單,is_hot = 0
的是一般書單喜德,然后按熱門書單按 create_time
排序山橄,普通書單也按 create_time
排序,返回一個集合住诸〖莸ǎ或者分頁查詢涣澡。我們該如何實現(xiàn)這個需求呢?查詢語句如下:
mysql> (select * from books where is_hot = 1 order by create_time desc limit 7)
-> union
-> (select * from books where is_hot = 0 order by create_time desc limit 7);
+----+---------------------------------------+---------------------+--------+
| id | name | create_time | is_hot |
+----+---------------------------------------+---------------------+--------+
| 8 | Pro Spring Boot | 2017-12-09 17:19:36 | 1 |
| 6 | Java 9 Data Structures and Algorithms | 2017-12-07 17:19:36 | 1 |
| 4 | The Ruby Programming | 2017-12-05 17:19:36 | 1 |
| 7 | The C Programming Language | 2017-12-08 17:19:36 | 0 |
| 5 | JavaScript Definitive Guides | 2017-12-06 17:19:36 | 0 |
| 1 | Head First Java | 2017-12-04 17:19:36 | 0 |
| 2 | Think in Java | 2017-12-02 17:19:36 | 0 |
| 3 | Clean Code | 2017-12-01 17:19:36 | 0 |
+----+---------------------------------------+---------------------+--------+
8 rows in set (0.00 sec)
在每一個查詢語句的后面都寫上 limit 7 (總條數(shù))
丧诺。那么分頁呢入桂?
mysql> (select id, name, is_hot, create_time from books where is_hot = 1 order by create_time desc limit 7)
-> union
-> (select id, name, is_hot, create_time from books where is_hot = 0 order by create_time desc limit 7)
-> limit 3 offset 0;
+----+---------------------------------------+--------+---------------------+
| id | name | is_hot | create_time |
+----+---------------------------------------+--------+---------------------+
| 8 | Pro Spring Boot | 1 | 2017-12-09 17:19:36 |
| 6 | Java 9 Data Structures and Algorithms | 1 | 2017-12-07 17:19:36 |
| 4 | The Ruby Programming | 1 | 2017-12-05 17:19:36 |
+----+---------------------------------------+--------+---------------------+
3 rows in set (0.00 sec)
mysql> (select id, name, is_hot, create_time from books where is_hot = 1 order by create_time desc limit 7)
-> union
-> (select id, name, is_hot, create_time from books where is_hot = 0 order by create_time desc limit 7)
-> limit 3 offset 3;
+----+------------------------------+--------+---------------------+
| id | name | is_hot | create_time |
+----+------------------------------+--------+---------------------+
| 7 | The C Programming Language | 0 | 2017-12-08 17:19:36 |
| 5 | JavaScript Definitive Guides | 0 | 2017-12-06 17:19:36 |
| 1 | Head First Java | 0 | 2017-12-04 17:19:36 |
+----+------------------------------+--------+---------------------+
3 rows in set (0.00 sec)
多表查詢
創(chuàng)建一張雜志表 magazines
并插入兩條數(shù)據(jù)。
mysql> desc magazines;
+-------------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+---------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(255) | NO | | NULL | |
| create_time | datetime | NO | | NULL | |
| is_hot | tinyint(2) unsigned | NO | | 0 | |
+-------------+---------------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
mysql> select * from magazines;
+----+--------------+---------------------+--------+
| id | name | create_time | is_hot |
+----+--------------+---------------------+--------+
| 1 | 男人裝 | 2017-12-05 09:28:06 | 0 |
| 2 | 程序員 | 2017-12-01 09:28:06 | 1 |
+----+--------------+---------------------+--------+
3 rows in set (0.00 sec)
假如需求是從兩張表或者多張表中查詢出結(jié)果集驳阎,并且分頁顯示抗愁。那么這時候 where
很顯然做不到,但是 union
卻可以輕松做到呵晚。例如:查詢 books
和 magazines
表中 is_hot = 1
的數(shù)據(jù)蜘腌。
mysql> select * from books where is_hot = 1
-> union
-> select * from magazines where is_hot = 1;
+----+---------------------------------------+---------------------+--------+
| id | name | create_time | is_hot |
+----+---------------------------------------+---------------------+--------+
| 4 | The Ruby Programming | 2017-12-05 17:19:36 | 1 |
| 6 | Java 9 Data Structures and Algorithms | 2017-12-07 17:19:36 | 1 |
| 8 | Pro Spring Boot | 2017-12-09 17:19:36 | 1 |
| 2 | 程序員 | 2017-12-01 09:28:06 | 1 |
+----+---------------------------------------+---------------------+--------+
4 rows in set (0.01 sec)
區(qū)分多表
聯(lián)合多張表查詢的時候,有時候會需要做到區(qū)分?jǐn)?shù)據(jù)來自于哪一張表饵隙。比如撮珠,查詢熱門雜志和熱門書籍,并且分別顯示來自于哪一張表金矛。
mysql> select *, 'books' as table_name from books where is_hot = 1
-> union
-> select *, 'magazines' as table_name from magazines where is_hot = 1;
+----+---------------------------------------+---------------------+--------+------------+
| id | name | create_time | is_hot | table_name |
+----+---------------------------------------+---------------------+--------+------------+
| 4 | The Ruby Programming | 2017-12-05 17:19:36 | 1 | books |
| 6 | Java 9 Data Structures and Algorithms | 2017-12-07 17:19:36 | 1 | books |
| 8 | Pro Spring Boot | 2017-12-09 17:19:36 | 1 | books |
| 2 | 程序員 | 2017-12-01 09:28:06 | 1 | magazines |
+----+---------------------------------------+---------------------+--------+------------+
4 rows in set (0.00 sec)
這樣查詢出來的結(jié)果集芯急,封裝成對象集合返回給前端,也方便前端對 books
或者 magazines
打不同的 tag
驶俊。
具體應(yīng)用場景
比如我們要對 books
表中的數(shù)據(jù)進(jìn)行分頁顯示娶耍,要求熱門書籍在上面,普通書籍在下面饼酿。熱門書籍按 create_time
倒序榕酒,普通書籍也按 create_time
倒序。然后分頁故俐,每頁顯示 5
條數(shù)據(jù)想鹰。
第一頁的數(shù)據(jù):
mysql> (select * from books where is_hot = 1 order by create_time desc limit 8)
-> union
-> (select * from books where is_hot = 0 order by create_time desc limit 8)
-> limit 5 offset 0;
+----+---------------------------------------+---------------------+--------+
| id | name | create_time | is_hot |
+----+---------------------------------------+---------------------+--------+
| 8 | Pro Spring Boot | 2017-12-09 17:19:36 | 1 |
| 6 | Java 9 Data Structures and Algorithms | 2017-12-07 17:19:36 | 1 |
| 4 | The Ruby Programming | 2017-12-05 17:19:36 | 1 |
| 7 | The C Programming Language | 2017-12-08 17:19:36 | 0 |
| 5 | JavaScript Definitive Guides | 2017-12-06 17:19:36 | 0 |
+----+---------------------------------------+---------------------+--------+
5 rows in set (0.01 sec)
第二頁的數(shù)據(jù):
mysql> (select * from books where is_hot = 1 order by create_time desc limit 8)
-> union
-> (select * from books where is_hot = 0 order by create_time desc limit 8)
-> limit 5 offset 5;
+----+-----------------+---------------------+--------+
| id | name | create_time | is_hot |
+----+-----------------+---------------------+--------+
| 1 | Head First Java | 2017-12-04 17:19:36 | 0 |
| 2 | Think in Java | 2017-12-02 17:19:36 | 0 |
| 3 | Clean Code | 2017-12-01 17:19:36 | 0 |
+----+-----------------+---------------------+--------+
3 rows in set (0.00 sec)
參考資料
https://dev.mysql.com/doc/refman/5.7/en/any-in-some-subqueries.html