【sql】union和union all的区别

时间:2022-01-07 15:25:58

union的主要作用是将多个select的结果进行合并,每个select的语句执行结果必须得满足如下要求才能进行union操作:

  1. UNION必须由两条或两条以上的SELECT语句组成,语句之间用关键字UNION分隔(因此,如果组合4条SELECT语句,将要使用3个UNION关键字);
  2. UNION中的每个查询必须包含相同的列、表达式或聚集函数(不过各个列不需要以相同的次序列出);
  3. 列数据类型必须兼容:类型不必完全相同,但必须是DBMS可以隐含地转换的类型(例如,不同的数值类型或不同的日期类型);

unionunion 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)