【sql】union和union all的区别

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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值