首页 > 数据库 >MySQL 合并查询union 查询出的行合并到一个表中

MySQL 合并查询union 查询出的行合并到一个表中

时间:2023-02-01 12:23:08浏览次数:58  
标签:25 01 union 合并 查询 2021 id

在合并查询中,尤其是二分类的情况,在查询结果是相同列名的时候可以考虑合并查询。先查询出行的结果,再使用union或者union all合并查询结果。

另外如果 union 和 order by 一起使用的话要注意使用方法。

一、适用场景和方法

(1)适用场景

考虑查询过程中是否存在以下情况:

  • 查询行时用的表不同;

  • 查询某些行时需要where条件,某些行不需要where条件;

  • 分类查询;

  • 查询的结果具有相同的列名。

存在上述情况时,大多数需要合并查询。先分行查询,再将查询出的行合并到一个表中。

(2)方法

MySQL合并查询,将查询到的行(具有相同列)合并到一个表中使用union或者union all函数

具体包括:

函数 使用说明
union 出现相同行时,不保留重复行,进行去重处理
union all 出现相同行时,保留重复行,不进行去重

根据查询需要使用不同合并函数。

二、案例分析

下面用2个具体的案例(由简到难)来说明行合并的过程:

(1)简单案例

案例来自:SQL26 计算25岁以上和以下的用户数量

描述

现在运营想要将用户划分为25岁以下和25岁及以上两个年龄段,分别查看这两个年龄段用户数量

本题注意:age为null 也记为 25岁以下

示例:user_profile

id device_id gender age university gpa active_days_within_30 question_cnt answer_cnt
1 2138 male 21 北京大学 3.4 7 2 12
2 3214 male 复旦大学 4 15 5 25
3 6543 female 20 北京大学 3.2 12 3 30
4 2315 female 23 浙江大学 3.6 5 1 2
5 5432 male 25 山东大学 3.8 20 15 70
6 2131 male 28 山东大学 3.3 15 7 13
7 4321 male 26 复旦大学 3.6 9 6 52

根据示例,你的查询应返回以下结果:

age_cut number
25岁以下 4
25岁及以上 3

【分类】:合并查询、多表连接

分析思路

难点:

1.单个字符或者值可以作为一列:例如'activity2' as activity

2.用了一半时间就完成高难度试卷。两个时间相减得到分钟:timestampdiff(minute, date_expr1, date_expr2) 两个时间的差

(1)统计25岁以下学生的人数

​ [条件]:where score >= 85 and year(start_time) = 2021

​ [使用]:distinct。一定要去重

(2)统计25岁以上学生的人数

​ [条件]:where difficulty = 'hard' and score > 80 and year(start_time) = 2021 and timestampdiff(minute, start_time, submit_time) < duration / 2

​ [使用]:多表连接使用 join using( )

(3)合并两个表

​ [使用]:union all 和union 都可以,因为列activity不会有重复。

最终结果

(
select 查询结果 [年龄段; 人数]
from 从哪张表中查询数据[用户表]
where 查询条件 [年龄小于25或者为空]
)
union 
(
select 查询结果 [年龄段; 人数]
from 从哪张表中查询数据[用户表]
where 查询条件 [年龄大于25]
)

该题的多种解法详见:SQL26 计算25岁以上和以下的用户数量

求解代码

union

(
#统计25岁以下学生的人数
select
    '25岁以下' as age_cut,
    count(device_id) as number
from user_profile
where age < 25 or age is null
)
union
(
#统计25岁以上学生的人数
select
    '25岁及以上' as age_cut,
    COUNT(device_id) as number
from user_profile
where age >= 25
)

(2)较难案例

案例来自:SQL132 每个题目和每份试卷被作答的人数和次数

描述

现有试卷作答记录表exam_record(uid用户ID, exam_id试卷ID, start_time开始作答时间, submit_time交卷时间, score得分):

id uid exam_id start_time submit_time score
1 1001 9001 2021-09-01 09:01:01 2021-09-01 09:41:01 81
2 1002 9002 2021-09-01 12:01:01 2021-09-01 12:31:01 70
3 1002 9001 2021-09-01 19:01:01 2021-09-01 19:40:01 80
4 1002 9002 2021-09-01 12:01:01 2021-09-01 12:31:01 70
5 1004 9001 2021-09-01 19:01:01 2021-09-01 19:40:01 85
6 1002 9002 2021-09-01 12:01:01 (NULL) (NULL)

题目练习表practice_record(uid用户ID, question_id题目ID, submit_time提交时间, score得分):

id uid question_id submit_time score
1 1001 8001 2021-08-02 11:41:01 60
2 1002 8001 2021-09-02 19:30:01 50
3 1002 8001 2021-09-02 19:20:01 70
4 1002 8002 2021-09-02 19:38:01 70
5 1003 8001 2021-08-02 19:38:01 70
6 1003 8001 2021-08-02 19:48:01 90
7 1003 8002 2021-08-01 19:38:01 80

请统计每个题目和每份试卷被作答的人数和次数,分别按照"试卷"和"题目"的uv & pv降序显示,示例数据结果输出如下:

tid uv pv
9001 3 3
9002 1 3
8001 3 5
8002 2 2

解释:“试卷”有3人共练习3次试卷9001,1人作答3次9002;“刷题”有3人刷5次8001,有2人刷2次8002

【分类】:合并查询

分析思路

难点:

  1. union 和 order by 一起使用需要注意的问题

(1)统计每份试卷被作答的人数和次数

​ [条件]:where score >= 85 and year(start_time) = 2021

​ [使用]:distinct。一定要去重

(2)统计每个题目被作答的人数和次数

​ [条件]:where difficulty = 'hard' and score > 80 and year(start_time) = 2021 and timestampdiff(minute, start_time, submit_time) < duration / 2

​ [使用]:多表连接使用 join using( )

(3)合并两个表,分别按照"试卷"和"题目"的uv & pv降序显示

​ [使用]:union all 和union 都可以,因为列activity不会有重复。

最终结果

select * from 
(
select 查询结果 [试卷ID; 作答次数]
from 从哪张表中查询数据[试卷作答记录表]
group by 分组条件 [试卷ID]
order by 对查询结果排序 [按照"试卷"的uv & pv降序]
)
union
select * from 
(
select 查询结果 [题目ID; 作答次数]
from 从哪张表中查询数据[题目练习表]
group by 分组条件 [题目ID]
order by 对查询结果排序 [按照"题目"的uv & pv降序]
)

求解代码

方法一:

#正确代码
select * from 
(
select 
    exam_id as tid,
    count(distinct uid) as uv,
    count(uid) as pv
from exam_record a
group by exam_id
order by uv desc, pv desc
) a
union
select * from 
(
select 
    question_id as tid,
    count(distinct uid) as uv,
    count(uid) as pv
from practice_record b
group by question_id
order by uv desc, pv desc
) attr

是不是可以union两个子句之后再使用order by ? 但是这个排序要对2个表分别进行降序,就需要写成下面这样:

方法二:

使用函数

left(str,length) 函数: str左边开始的长度为 length 的子字符串,在本例中为‘9’和‘8’

解释:试卷编号以‘9’开头、题目编号以‘8’开头,对编号进行降序就是对"试卷"和"题目"分别进行排序

(
#每份试卷被作答的人数和次数
select
    exam_id as tid,
    count(distinct uid) as uv,
    count(*) as pv
from exam_record
group by exam_id
)
union
(
#每个题目被作答的人数和次数
select
    question_id as tid,
    count(distinct uid) as uv,
    count(*) as pv
from practice_record
group by question_id
)
#分别按照"试卷"和"题目"的uv & pv降序显示
order by left(tid,1) desc,uv desc,pv desc

推荐使用方法一,更具有普适性。

扩展:

前往查看MySQL union 和 order by 一起使用需要注意的问题

标签:25,01,union,合并,查询,2021,id
From: https://www.cnblogs.com/chengyj/p/17082139.html

相关文章

  • python mongo查询
    importpymongo#连接数据库环境myclient1=pymongo.MongoClient('mongodb://账号:密码@ip:端口/')mydb1=myclient1["slot"]//dbmycol1=mydb1["ota.versions"]//表x=my......
  • Hive 刷题——查询每个用户登录日期的最大空档期
    需求描述从登录明细表(user_login_detail)中查询每个用户两个登录日期(以login_ts为准)之间的最大的空档期。统计最大空档期时,用户最后一次登录至今的空档也要考虑在内,假设今......
  • xml解析_Jsoup_根据选择器查询、根据Xpath查询
    xml解析_Jsoup_根据选择器查询快捷查询方式:selector:选择器使用的方法:Elements select(String cssQuery)语法:参考Selector类中定义的语法......
  • 【mysql】 查询数据时group by,及group_concat()函数用法
    GROUPBY语句语法select聚合函数,列(要求出现在groupby的后面)from表where筛选条......
  • 合并两个有序链表
    /***Definitionforsingly-linkedlist.*functionListNode(val,next){*this.val=(val===undefined?0:val)*this.next=(next===undefined......
  • 国内网站 ICP 备案号查询工具 All In One
    国内网站ICP备案号查询工具AllInOneICP备案号查询公安机关互联网站安全管理服务平台https://www.beian.gov.cn/portal/registerSystemInfodemosheytapimage.......
  • sql server 查询日期中的常用语句
    --本周第一天SELECTDATEADD(Day,1-(DATEPART(Weekday,getdate())+@@DATEFIRST-1)%7,getdate())--orselectdateadd(wk,datediff(wk,0,getdate()),0)--本......
  • [Typescript] Function Overloads vs. Union Types
    Uniontype:functionrunGenerator(generator:{run:()=>string}|(()=>string)){if(typeofgenerator==='function'){returngenerator();}r......
  • MySQL查询不区分大小写的问题总结
    MySQL查询不区分大小写问题1.问题描述最近,笔者在开发FleaAuth模块的过程中,准备验证以注册的账号huazie进行登录的场景,结果以HUAZIE成功登录,而数据库中存储的账号是hua......
  • CSS 媒体查询
    CSS媒体查询是CSS3中的一个功能,允许您为不同的媒体类型(例如屏幕,打印机,语音合成器等)和媒体特征(例如屏幕大小,分辨率,方向等)设置不同的样式。例如,如果您想要仅在屏幕宽度不......