MySQL UNION && UNION ALLmysql
http://blog.itpub.net/29254281/viewspace-1190027/sql
http://my.oschina.net/xinxingegeya/blog/225646數據庫
創建以下表,spa
drop table test; create table test( id int not null auto_increment, nickname varchar(20), playNum varchar(20), type int not null, primary key (id) ); insert into test(nickname,playNum,type) values('hello world',10,1); insert into test(nickname,playNum,type) values('hello world',10,2); insert into test(nickname,playNum,type) values('hello world',10,3); insert into test(nickname,playNum,type) values('hello world',10,4); insert into test(nickname,playNum,type) values('hello world',10,5); insert into test(nickname,playNum,type) values('hello world',20,6);
type 分別爲偶數 或 奇數 的查詢結構,.net
mysql> select distinct nickname , playNum from test where type % 2 = 0; +-------------+---------+ | nickname | playNum | +-------------+---------+ | hello world | 10 | | hello world | 20 | +-------------+---------+ 2 rows in set (0.00 sec) mysql> select distinct nickname , playNum from test where type % 2 != 0; +-------------+---------+ | nickname | playNum | +-------------+---------+ | hello world | 10 | +-------------+---------+ 1 row in set (0.00 sec)
這兩個查詢結果的交集爲 ,使用以下sql 查詢,code
select nickname ,playNum ,count(*) from (select nickname , playNum from test where type % 2 = 0 union all select nickname , playNum from test where type % 2 != 0) temp group by nickname , playNum having count(*) >1;
以下查詢結果,blog
mysql> select nickname ,playNum ,count(*) from (select nickname , playNum from test where type % 2 = 0 union all select nickname , playNum from test where type % 2 != 0) temp group by nickname , pla yNum having count(*) >1; +-------------+---------+----------+ | nickname | playNum | count(*) | +-------------+---------+----------+ | hello world | 10 | 5 | +-------------+---------+----------+ 1 row in set (0.00 sec)
UNION屬於集合運算符(set operator)容許咱們把多個表表達式組合到一個複合表表達式中,它把一個表表達式的結果放在另外一個表表達式的下面。rem
在mysql數據庫中提供了UNION和UNION ALL關鍵字,列於每一個SELECT語句的對應位置的被選擇的列應具備相同的類型。get
在第一個SELECT語句中被使用的列名稱也被用於結果的列名稱。it
若是UNION不使用關鍵詞ALL,則全部返回的行都是惟一的,如同已經對整個結果集合使用了DISTINCT。
若是指定了ALL,則會從全部用過的SELECT語句中獲得全部匹配的行。DISTINCT關鍵詞是一個自選詞,不起任何做用,可是根據SQL標準的要求,在語法中容許採用。也能夠在同一查詢中混合UNION ALL和UNION DISTINCT。被混合的UNION類型按照這樣的方式對待,即DISTICT共用體覆蓋位於其左邊的全部ALL共用體。DISTINCT共用體能夠使用UNION DISTINCT明確地生成,或使用UNION(後面不加DISTINCT或ALL關鍵詞)隱含地生成。
仍是如上面的表,union 查詢和union all查詢的區別,
mysql> select nickname , playNum from test where type % 2 = 0 union all select nickname , playNum from test where type % 2 != 0; +-------------+---------+ | nickname | playNum | +-------------+---------+ | hello world | 10 | | hello world | 10 | | hello world | 20 | | hello world | 10 | | hello world | 10 | | hello world | 10 | +-------------+---------+ 6 rows in set (0.00 sec) mysql> select nickname , playNum from test where type % 2 = 0 union select nickname , playNum from test where type % 2 != 0; +-------------+---------+ | nickname | playNum | +-------------+---------+ | hello world | 10 | | hello world | 20 | +-------------+---------+ 2 rows in set (0.00 sec)
=============END=============