MySQL UNION && UNION ALL

本文介绍了如何在MySQL数据库中通过UNION和UNIONALL关键字查询特定类型的记录,并详细解释了它们的区别。通过实例演示了查询偶数和奇数类型记录的交集操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

MySQL UNION && UNION ALL

http://blog.itpub.net/29254281/viewspace-1190027/

http://my.oschina.net/xinxingegeya/blog/225646

建立如下表,

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 分别为偶数 或 奇数 的查询结构,

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 查询,

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;

如下查询结果,

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 和 UNION ALL的区别

UNION属于集合运算符(set operator)允许我们把多个表表达式组合到一个复合表表达式中,它把一个表表达式的结果放在另一个表表达式的下面。

在mysql数据库中提供了UNION和UNION ALL关键字,列于每个SELECT语句的对应位置的被选择的列应具有相同的类型。

在第一个SELECT语句中被使用的列名称也被用于结果的列名称。

如果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=============

转载于:https://my.oschina.net/xinxingegeya/blog/476326

### MySQLUNIONUNION ALL 的区别及用法 #### 1. 定义与功能 UNIONUNION ALL 都用于将两个或多个 SELECT 查询的结果集合并成一个单一的结果集。然而,它们的主要区别在于如何处理重复的记录。 - **UNION**: 将结果集合并并自动去重,即会执行 DISTINCT 操作来移除重复的行[^3]。 - **UNION ALL**: 直接将所有结果集简单拼接在一起,不会进行任何额外的操作,因此可能会保留重复的行[^4]。 #### 2. 使用条件 为了能够正确使用 UNIONUNION ALL,需满足以下条件: - 所有 SELECT 子句中的列数必须一致。 - 列的数据类型应兼容或可隐式转换。 - 各子查询中列的顺序应当保持一致[^2]。 #### 3. 性能比较 由于 UNION 进行了去重操作,其性能通常低于 UNION ALL。如果可以确定结果集中不存在重复项或者无需考虑重复数据的情况,推荐优先使用 UNION ALL 来提高查询效率[^4]。 #### 4. 结果排序 当使用 UNION 时,默认会对最终结果按照字段顺序进行排序;而 UNION ALL 不会对结果做任何排序处理,仅按原始顺序返回数据[^4]。 以下是具体的 SQL 示例: ```sql -- 创建测试表 Chinese 和 Math CREATE TABLE Chinese ( name VARCHAR(50), score INT ); INSERT INTO Chinese VALUES ('xiaoming', 80), ('xiaohong', 78), ('xiaowen', 92); CREATE TABLE Math ( name VARCHAR(50), score INT ); INSERT INTO Math VALUES ('xiaoming', 90), ('xiaohong', 85), ('xiaoliu', 88); ``` ##### 使用 UNION 去重 ```sql SELECT name, 'Chinese' AS subject, score FROM Chinese UNION SELECT name, 'Math' AS subject, score FROM Math; ``` 此查询会先合并两部分数据再通过 DISTINCT 移除可能存在的重复姓名及其对应成绩组合[^3]。 ##### 使用 UNION ALL 不去重 ```sql SELECT name, 'Chinese' AS subject, score FROM Chinese UNION ALL SELECT name, 'Math' AS subject, score FROM Math; ``` 该命令直接叠加两条独立查询所得的所有记录,即使存在完全相同的行也不会被剔除。 #### 5. 特殊注意事项 如果在联合查询过程中涉及别名定义,则 ORDER BY 子句需要引用这些已声明好的别名才能正常工作[^1]。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值