Mysql的聯合查詢命令UNION和UNION ALL

Mysql的聯合查詢命令UNION和UNION ALL

SQL UNION 語法:
    SELECT column_name FROM table1
    UNION
    SELECT column_name FROM table2
註釋:默認UNION 操作符選取不同的值.如果允許重複的值,請使用 UNION ALL.
   當 ALL 隨 UNION 一起使用時(即 UNION ALL),不消除重複行.
SQL UNION ALL 語法
    SELECT column_name FROM table1
    UNION ALL
    SELECT column_name FROM table2
註釋:UNION 結果集中的列名總是等於 UNION 中第一個 SELECT 語句中的列名.
注意:UNION 結果集中的列名總是等於第一個 SELECT 語句中的列名.

#兩張表的數據如下:
mysql> select hid,bname from boy where hid=1;
+-----+-------+
| hid | bname |
+-----+-------+
|   1 | lisi  |
+-----+-------+
1 row in set (0.00 sec)

mysql> select hid,bname from boy where hid=3;
+-----+--------+
| hid | bname  |
+-----+--------+
|   3 | 趙六   |
+-----+--------+
1 row in set (0.00 sec)

mysql> select hid,bname from boy where hid=1 union select hid,bname from boy where hid=3;
+-----+--------+
| hid | bname  |
+-----+--------+
|   1 | lisi   |
|   3 | 趙六   |
+-----+--------+
2 rows in set (0.00 sec)

#union語句必須滿足1個條件;各語句取出的列數相同.
mysql> select hid,bname from boy where hid=1
    -> union
    -> select bname from boy where hid=3;
ERROR 1222 (21000): The used SELECT statements have a different number of columns
 

#union列名稱未必要一致,列名稱會使用第一條sql的列名稱爲準.
mysql> select hid,bname from boy where hid=1;
+-----+-------+
| hid | bname |
+-----+-------+
|   1 | lisi  |
+-----+-------+
1 row in set (0.00 sec)

mysql> select cno,score from sc where cno="k1";
+-----+-------+
| cno | score |
+-----+-------+
| K1  |    83 |
| K1  |    85 |
| K1  |    92 |
+-----+-------+
3 rows in set (0.00 sec)

mysql> select hid,bname from boy where hid=1
    -> union
    -> select cno,score from sc where cno="k1";
+-----+-------+
| hid | bname |
+-----+-------+
| 1   | lisi  |
| K1  | 83    |
| K1  | 85    |
| K1  | 92    |
+-----+-------+
4 rows in set (0.00 sec)

#union查詢支持跨表查詢.
mysql> select hid,bname from boy where hid=1;
+-----+-------+
| hid | bname |
+-----+-------+
|   1 | lisi  |
+-----+-------+
1 row in set (0.01 sec)

mysql> select hid,bname from girl where hid=3;
+-----+--------+
| hid | bname  |
+-----+--------+
|   3 | 默默   |
+-----+--------+
1 row in set (0.00 sec)

mysql> select hid,bname from boy where hid=1 union select hid,bname from girl where hid=3;
+-----+--------+
| hid | bname  |
+-----+--------+
|   1 | lisi   |
|   3 | 默默   |
+-----+--------+
2 rows in set (0.00 sec)

#girl表3條數據.
mysql> select * from girl;
+-----+--------+
| hid | bname  |
+-----+--------+
|   3 | 默默   |
|   2 | 羞羞   |
|   5 | 海燕   |
+-----+--------+
3 rows in set (0.00 sec)

#boy表4條數據.
mysql> select * from boy;
+-----+--------+
| hid | bname  |
+-----+--------+
|   1 | lisi   |
|   2 | 王五   |
|   3 | 趙六   |
|   5 | 海燕   |
+-----+--------+
4 rows in set (0.00 sec)

#查詢結果只有7條數據?
mysql> select * from girl
    -> union
    -> select * from boy;
+-----+--------+
| hid | bname  |
+-----+--------+
|   3 | 默默   |
|   2 | 羞羞   |
|   5 | 海燕   |
|   1 | lisi   |
|   2 | 王五   |
|   3 | 趙六   |
+-----+--------+
6 rows in set (0.00 sec)
注意:使用union時,完全相等的行將會被合併,合併是比較耗時的操作,一般不讓union進行合併,使用"union all"避免合併.

#使用"union all"相同的行將不會被合併.
mysql> select * from girl union  all select * from boy;
+-----+--------+
| hid | bname  |
+-----+--------+
|   3 | 默默   |
|   2 | 羞羞   |
|   5 | 海燕   |
|   1 | lisi   |
|   2 | 王五   |
|   3 | 趙六   |
|   5 | 海燕   |
+-----+--------+
7 rows in set (0.00 sec)

#union的子句中不寫order by,如下面sql,結果並未體現出order by排序的結果.
mysql> (select * from girl order by hid desc) union  all (select * from boy order by hid desc);
+-----+--------+
| hid | bname  |
+-----+--------+
|   3 | 默默   |
|   2 | 羞羞   |
|   5 | 海燕   |
|   1 | lisi   |
|   2 | 王五   |
|   3 | 趙六   |
|   5 | 海燕   |
+-----+--------+
7 rows in set (0.00 sec)

#union sql合併後得到的總的結果,可以使用order by,子句order by失去意義.
mysql> select * from girl union  all select * from boy order by hid desc;
+-----+--------+
| hid | bname  |
+-----+--------+
|   5 | 海燕   |
|   5 | 海燕   |
|   3 | 默默   |
|   3 | 趙六   |
|   2 | 王五   |
|   2 | 羞羞   |
|   1 | lisi   |
+-----+--------+
7 rows in set (0.00 sec)

#下面是兩張表合併的數據,將其中bname相同的行的hid求和.
mysql> select * from boy union all select * from girl as tmp;
+-----+--------+
| hid | bname  |
+-----+--------+
|   1 | lisi   |
|   2 | 王五   |
|   3 | 趙六   |
|   5 | 海燕   |
|   3 | 默默   |
|   2 | 羞羞   |
|   5 | 海燕   |
+-----+--------+
7 rows in set (0.00 sec)

#將兩張表的查詢結果看成一張表,使用group by分組求和.
mysql> select sum(hid),bname from (select * from boy union all select * from girl) as tmp group by bname;
+----------+--------+
| sum(hid) | bname  |
+----------+--------+
|        1 | lisi   |
|       10 | 海燕   |
|        2 | 王五   |
|        2 | 羞羞   |
|        3 | 趙六   |
|        3 | 默默   |
+----------+--------+
6 rows in set (0.00 sec)


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章