【sql】union和union all的區別
阿新 • • 發佈:2018-12-23
union的主要作用是將多個select的結果進行合併,每個select的語句執行結果必須得滿足如下要求才能進行union操作:
- UNION必須由兩條或兩條以上的SELECT語句組成,語句之間用關鍵字UNION分隔(因此,如果組合4條SELECT語句,將要使用3個UNION關鍵字);
- UNION中的每個查詢必須包含相同的列、表示式或聚集函式(不過各個列不需要以相同的次序列出);
- 列資料型別必須相容:型別不必完全相同,但必須是DBMS可以隱含地轉換的型別(例如,不同的數值型別或不同的日期型別);
union和union all的主要區別在於前者會去掉重複的記錄,後者不會,
例如:
mysql> select * from products where prod_price <=5; --結果4條
+---------+---------+---------------+------------+--------------------------------------+
| prod_id | vend_id | prod_name | prod_price | prod_desc |
+---------+---------+---------------+------------+--------------------------------------+
| FC | 1003 | Carrots | 2.50 | Carrots (rabbit hunting season only) |
| FU1 | 1002 | Fuses | 3.42 | 1 dozen, extra long |
| SLING | 1003 | Sling | 4.49 | Sling, one size fits all |
| TNT1 | 1003 | TNT (1 stick) | 2.50 | TNT, red, single stick |
+---------+---------+---------------+------------+--------------------------------------+
mysql> select * from products where vend_id = 1001 or vend_id = 1002; --結果5條
+---------+---------+--------------+------------+----------------------------------------------------------------+
| prod_id | vend_id | prod_name | prod_price | prod_desc |
+---------+---------+--------------+------------+----------------------------------------------------------------+
| ANV01 | 1001 | .5 ton anvil | 5.99 | .5 ton anvil, black, complete with handy hook |
| ANV02 | 1001 | 1 ton anvil | 9.99 | 1 ton anvil, black, complete with handy hook and carrying case |
| ANV03 | 1001 | 2 ton anvil | 14.99 | 2 ton anvil, black, complete with handy hook and carrying case |
| FU1 | 1002 | Fuses | 3.42 | 1 dozen, extra long |
| OL1 | 1002 | Oil can | 8.99 | Oil can, red |
+---------+---------+--------------+------------+----------------------------------------------------------------+
mysql> select * from products where prod_price <=5
-> union
-> select * from products where vend_id = 1001 or vend_id = 1002; --去掉重複行FU1,所有現在是8條
+---------+---------+---------------+------------+----------------------------------------------------------------+
| prod_id | vend_id | prod_name | prod_price | prod_desc |
+---------+---------+---------------+------------+----------------------------------------------------------------+
| FC | 1003 | Carrots | 2.50 | Carrots (rabbit hunting season only) |
| FU1 | 1002 | Fuses | 3.42 | 1 dozen, extra long |
| SLING | 1003 | Sling | 4.49 | Sling, one size fits all |
| TNT1 | 1003 | TNT (1 stick) | 2.50 | TNT, red, single stick |
| ANV01 | 1001 | .5 ton anvil | 5.99 | .5 ton anvil, black, complete with handy hook |
| ANV02 | 1001 | 1 ton anvil | 9.99 | 1 ton anvil, black, complete with handy hook and carrying case |
| ANV03 | 1001 | 2 ton anvil | 14.99 | 2 ton anvil, black, complete with handy hook and carrying case |
| OL1 | 1002 | Oil can | 8.99 | Oil can, red |
+---------+---------+---------------+------------+----------------------------------------------------------------
mysql> select * from products where prod_price <=5
-> union all
-> select * from products where vend_id = 1001 or vend_id = 1002; -- 並沒有去掉重複行
+---------+---------+---------------+------------+----------------------------------------------------------------+
| prod_id | vend_id | prod_name | prod_price | prod_desc |
+---------+---------+---------------+------------+----------------------------------------------------------------+
| FC | 1003 | Carrots | 2.50 | Carrots (rabbit hunting season only) |
| FU1 | 1002 | Fuses | 3.42 | 1 dozen, extra long |
| SLING | 1003 | Sling | 4.49 | Sling, one size fits all |
| TNT1 | 1003 | TNT (1 stick) | 2.50 | TNT, red, single stick |
| ANV01 | 1001 | .5 ton anvil | 5.99 | .5 ton anvil, black, complete with handy hook |
| ANV02 | 1001 | 1 ton anvil | 9.99 | 1 ton anvil, black, complete with handy hook and carrying case |
| ANV03 | 1001 | 2 ton anvil | 14.99 | 2 ton anvil, black, complete with handy hook and carrying case |
| FU1 | 1002 | Fuses | 3.42 | 1 dozen, extra long |
| OL1 | 1002 | Oil can | 8.99 | Oil can, red |
+---------+---------+---------------+------------+----------------------------------------------------------------+
9 rows in set (0.00 sec)