温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

MySQL中将多个select语句的查询结果合并一起的方法

发布时间:2020-05-25 17:32:34 阅读:3577 作者:三月 栏目:MySQL数据库
亿速云mysql数据库,读写分离,安全稳定,弹性扩容,低至0.3元/天!! 点击查看>>

下文给大家带来有关MySQL中将多个select语句的查询结果合并一起的方法内容,相信大家一定看过类似的文章。我们给大家带来的有何不同呢?一起来看看正文部分吧,相信看完MySQL中将多个select语句的查询结果合并一起的方法你一定会有所收获。

1. 背景

   * 全并查询结果是将多个 select 语句的查询结果合并到一起。

   * 参与合并的结果集需要字段统一。

   * 字段可以用空字符串''代替。

2. 合并查询结果实战 [ users1 and users2 ]

   * 查看 users1 表和 users2 表结构

mysql> desc users1;
+-------+---------------+------+-----+---------+----------------+
| Field | Type          | Null | Key | Default | Extra          |
+-------+---------------+------+-----+---------+----------------+
| id    | bigint(20)    | NO   | PRI | NULL    | auto_increment |
| name  | varchar(64)   | NO   |     | NULL    |                |
| sex   | enum('M','F'| NO   |     | NULL    |                |
| age   | int(11)       | NO   |     | NULL    |                |
+-------+---------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)

mysql> desc users2;
+-------+---------------+------+-----+---------+----------------+
| Field | Type          | Null | Key | Default | Extra          |
+-------+---------------+------+-----+---------+----------------+
| id    | bigint(20)    | NO   | PRI | NULL    | auto_increment |
| name  | varchar(64)   | NO   |     | NULL    |                |
| sex   | enum('M','F'| NO   |     | NULL    |                |
| age   | int(11)       | NO   |     | NULL    |                |
+-------+---------------+------+-----+---------+----------------+
4 rows in set (0.01 sec)

   * 查看 users1 表和 users2 表数据

    users1和users2表中有相同字段 tom

mysql> select * from users1;
+----+------+-----+-----+
| id | name | sex | age |
+----+------+-----+-----+
|  1 | tom  | M   |  25 |
|  2 | jak  | F   |  42 |
+----+------+-----+-----+
2 rows in set (0.00 sec)

mysql> select * from users2;
+----+-------+-----+-----+
| id | name  | sex | age |
+----+-------+-----+-----+
|  1 | tom   | M   |  25 |
|  2 | lisea | M   |  42 |
+----+-------+-----+-----+
2 rows in set (0.00 sec)

   * union 合并并去重

MySQL中将多个select语句的查询结果合并一起的方法

mysql> (select * from users1) union (select * from users2);
+----+-------+-----+-----+
| id | name  | sex | age |
+----+-------+-----+-----+
|  1 | tom   | M   |  25 |
|  2 | jak   | F   |  42 |
|  2 | lisea | M   |  42 |
+----+-------+-----+-----+
3 rows in set (0.00 sec)

   * union all 只全并不去重

MySQL中将多个select语句的查询结果合并一起的方法

mysql> (select * from users1) union all (select * from users2);
+----+-------+-----+-----+
| id | name  | sex | age |
+----+-------+-----+-----+
|  1 | tom   | M   |  25 |
|  2 | jak   | F   |  42 |
|  1 | tom   | M   |  25 |
|  2 | lisea | M   |  42 |
+----+-------+-----+-----+
4 rows in set (0.01 sec)

   * 查看union  性能分析

      [ 使用了临时表 ]

mysql> explain (select * from users1) union (select * from users2);
+----+--------------+------------+------------+------+---------------+------+---------+------+------+----------+-----------------+
| id | select_type  | table      | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra           |
+----+--------------+------------+------------+------+---------------+------+---------+------+------+----------+-----------------+
|  1 | PRIMARY      | users1     | NULL       | ALL  | NULL          | NULL | NULL    | NULL |    2 |   100.00 | NULL            |
|  2 | UNION        | users2     | NULL       | ALL  | NULL          | NULL | NULL    | NULL |    2 |   100.00 | NULL            |
| NULL | UNION RESULT | <union1,2> | NULL       | ALL  | NULL        | NULL | NULL    | NULL | NULL |     NULL | Using temporary |
+----+--------------+------------+------------+------+---------------+------+---------+------+------+----------+-----------------+
3 rows in set1 warning (0.01 sec)

   * 查看union all 性能分析

     [ 未使用临时表 ]

mysql> explain (select * from users1) union all (select * from users2);
+----+-------------+--------+------------+------+---------------+------+---------+------+------+----------+-------+
| id | select_type | table  | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra |
+----+-------------+--------+------------+------+---------------+------+---------+------+------+----------+-------+
|  1 | PRIMARY     | users1 | NULL       | ALL  | NULL          | NULL | NULL    | NULL |    2 |   100.00 | NULL  |
|  2 | UNION       | users2 | NULL       | ALL  | NULL          | NULL | NULL    | NULL |    2 |   100.00 | NULL  |
+----+-------------+--------+------------+------+---------------+------+---------+------+------+----------+-------+
2 rows in set1 warning (0.01 sec)

3. union 与 union all 总结

   * union 相对于 union all多了一步去重操作,此操作会创建临时表,降低性能。

   * 当两边结果集数据相对都确定了唯一性,推荐使用union all。

4. 总结

以需求驱动技术,技术本身没有优略之分,只有业务之分。

对于上文关于MySQL中将多个select语句的查询结果合并一起的方法,大家觉得是自己想要的吗?如果想要了解更多相关,可以继续关注我们的行业资讯板块。

亿速云「云数据库 MySQL」免部署即开即用,比自行安装部署数据库高出1倍以上的性能,双节点冗余防止单节点故障,数据自动定期备份随时恢复。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI

开发者交流群×