MySQL中将多个select语句的查询结果合并一起的方法
发表于:2024-10-11 作者:千家信息网编辑
千家信息网最后更新 2024年10月11日,下文给大家带来有关MySQL中将多个select语句的查询结果合并一起的方法内容,相信大家一定看过类似的文章。我们给大家带来的有何不同呢?一起来看看正文部分吧,相信看完MySQL中将多个select语
千家信息网最后更新 2024年10月11日MySQL中将多个select语句的查询结果合并一起的方法
下文给大家带来有关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 * 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 * 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 || NULL | ALL | NULL | NULL | NULL | NULL | NULL | NULL | Using temporary |+----+--------------+------------+------------+------+---------------+------+---------+------+------+----------+-----------------+3 rows in set, 1 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 set, 1 warning (0.01 sec)
3. union 与 union all 总结
* union 相对于 union all多了一步去重操作,此操作会创建临时表,降低性能。
* 当两边结果集数据相对都确定了唯一性,推荐使用union all。
4. 总结
以需求驱动技术,技术本身没有优略之分,只有业务之分。
对于上文关于MySQL中将多个select语句的查询结果合并一起的方法,大家觉得是自己想要的吗?如果想要了解更多相关,可以继续关注我们的行业资讯板块。
结果
查询
多个
语句
中将
方法
字段
性能
性能分析
技术
数据
分析
不同
相同
上文
下文
业务
会创
内容
只有
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
rd软件开发是什么
网络安全排查总结报告
长江大学考研数据库真题
辽宁省网络安全教育平台登录
多台电脑外部保存数据库
网络安全进校园名言警句
查找上海的各工程名数据库
饥荒进房间服务器
网络安全法 发帖
交警开展网络安全大检查
微信投票cms软件开发
五华区第三方软件开发咨询报价
服务器统一管理规定
服务器租用钜讯
火绒安全服务器异常该怎么办
数据库导出wincc数据
枣庄软件开发企业
传统软件开发方法
软件开发经理指标详解
c 提取相同的数据库
全国手机号码归属地数据库下载
补丁下载软件开发
中国最大软件开发公司
查看hive数据库
中联服务器管理工具安装教程
软件开发是铁饭碗吗
微信里的培训档案数据库
雷霆传奇数据库连接不上
公安调取网络安全日志
小学加强网络安全教育