sql多表查询之合并查询(union)

2023-05-16

题1

https://www.nowcoder.com/practice/203d0aed8928429a8978185d9a03babc?tpId=240&tags=&title=&difficulty=0&judgeStatus=0&rp=0

解1

(select 
    exam_id as tid,
    count(distinct uid) as uv,
    count(exam_id) as pv
FROM
    exam_record
group by
    exam_id)
    
union all # 不去重

(select 
    question_id as tid,
    count(distinct uid) as uv,
    count(question_id) as pv
FROM
    practice_record
group by
    question_id)

order  BY
    LEFT(tid, 1) desc,
    uv desc,
    pv DESC;

解2

select 
    * 
from
    (select 
        exam_id as tid,
        count(distinct uid) as uv,
        count(*) as pv
    from 
         exam_record
    group by 
         exam_id
    order by 
         uv desc, 
         pv desc) as exam
     
union

select 
    * 
from
    (select 
        question_id as tid,
        count(distinct uid) as uv,
        count(*) as pv
    from 
         practice_record
    group by 
         question_id
    order by 
         uv desc, 
         pv desc) as practice # 注意子表必须有表名。

解3

用tid字段的左边第一个数来排序。

要注意的是关于UNION后的排序问题,要么在UNION之前分别单独排序(如上解法),要么在union之后再排序:

(SELECT 
    exam_id AS tid, 
    COUNT(DISTINCT exam_record.uid) uv,
    COUNT(*) pv 
FROM 
    exam_record
GROUP BY 
    exam_id)
 
UNION
 
(SELECT 
    question_id AS tid, 
    COUNT(DISTINCT practice_record.uid) uv,
    COUNT(*) pv 
FROM 
    practice_record
GROUP BY 
    question_id)
  
ORDER BY LEFT(tid,1) DESC, uv DESC, pv DESC;

题2

https://www.nowcoder.com/practice/a126cea91d7045e399b8ecdcadfb326f?tpId=240&tags=&title=&difficulty=0&judgeStatus=0&rp=0

select 
    uid,
    'activity1' as activity
from 
    exam_record
where 
    year(submit_time) = 2021
group by 
    uid
having 
    min(score) >= 85 # 对于每组 要求最小得分不小于85,过滤
     
union all  # 将两个筛选合并。不去重

select 
    distinct uid,
    'activity2' as activity
from 
    exam_record e_r join examination_info e_i
    on e_r.exam_id = e_i.exam_id
where 
    year(e_r.submit_time) = 2021
    and e_i.difficulty = 'hard'
    and e_r.score > 80
    and timestampdiff(minute, e_r.start_time, e_r.submit_time) * 2 < e_i.duration
order by 
    uid

或:

select 
    uid,
    'activity1' as activity
from 
    exam_record
where 
    year(submit_time) = 2021
group by 
    uid
having 
    min(score) >= 85 # 对于每组 要求最小得分不小于85,过滤
     
union all  # 将两个筛选合并。不去重

select 
    distinct uid,
    'activity2' as activity
from 
    exam_record e_r join examination_info e_i
    on e_r.exam_id = e_i.exam_id
where 
    year(submit_time) = 2021
    and difficulty = 'hard'
    and score > 80
    and timestampdiff(minute, start_time, submit_time) * 2 < duration
order by 
    uid

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

sql多表查询之合并查询(union) 的相关文章

随机推荐

  • CodeForces - 719A Vitya in the Countryside(暴力)

    题目 传送门 思路 因为数据范围很小 xff0c 我们可以直接暴力 xff0c 我们以30天每天都为起点去与所给序列比较 xff0c 如果存在一天为起点时整个序列都是符合的 xff0c 那么比较下一天和最后一天的大小就可以了 这里我们要加一
  • 第十三周作业-必做3

    题目描述 xff1a 在大家不辞辛劳的帮助下 xff0c TT 顺利地完成了所有的神秘任务 神秘人很高兴 xff0c 决定给 TT 一个奖励 xff0c 即白日做梦之捡猫咪游戏 捡猫咪游戏是这样的 xff0c 猫咪从天上往下掉 xff0c
  • CodeForces 1165-B Polycarp Training

    题目 传送门 思路 将所有比赛进项排序 对于 第k天 xff0c 我们从贪心的角度出发肯定要选最接近 k 题的 比赛 不能比k题小 这样的话第 k 1天所选比赛的题数小于等于 k 天 的比赛题数 xff0c 所以我们的这个方法的复杂度是线性
  • gym 102302 2019 USP-ICMC H-Log Concave Sequences (dp + 矩阵快速幂优化)

    题目 传送门 思路 我们可以先写出转移方程 xff0c 发现该方程是一个不变的递推式 我们考虑用矩阵快速幂来优化这个递推式 完结撒花 AC Code span class token macro property span class to
  • Maven配置打包的jar或者war文件到指定目录

    最近项目打包比较频繁 xff0c 而且使用maven打包之后生成的jar包文件的都在不同项目的根目录的target目录下 xff0c 项目发布时候来回拷贝 xff0c 着实蛋疼 xff0c 所以就考虑把所有的项目到集中打包到一个目录里面 x
  • windows远程桌面连接树莓派通过xrdp服务

    远程桌面协议 xff08 RDP xff09 是微软的专有协议 xff0c 它利用低带宽连接来提供对桌面的访问 为了允许在树莓派上使用RDP xff0c 我们将使用一个名为xrdp的软件 xrdp软件将你的屏幕和格式化为他们的RDP实现 在
  • windows下Anaconda更改默认python环境的方法

    windows Linux下Anaconda更改默认python环境的方法 更改anaconda安装目录下 anaconda3 Scripts activate bat文件 将第24行 span class token decorator
  • 文献管理软件Zotero常用插件安装及配置使用

    文献管理软件 Zotero常用插件安装及配置使用 一 Zotero安装与同步盘配置1 下载Zotero并安装2 配置Zotero xff08 1 xff09 配置同步盘 xff08 以onedrive为例 xff09 如果不配置同步盘 xf
  • Github本地仓库使用学习记录

    一 注册Github账号 在官网注册github的账号 xff1a https github com 二 下载git本地客户端并安装 Windows 三个平台下载地址 xff1a http git scm com downloads 国内的
  • Win10+GTX 1660 SUPER安装Cuda11.5.1+cudnn8.3.0

    Win10 43 GTX 1660 SUPER安装Cuda11 5 1 43 cudnn8 3 0 一 cuda11 5 1安装步骤1 查看电脑的显卡驱动2 下载显卡驱动3 下载需要的cuda版本 二 对应版本Cudnn安装1 注册nvid
  • python的列表数据写入excel表

    python的列表数据写入excel表 将python代码生成的一个列表数值导入到excel发现按照行列排列不能全部输出到excel表的一列当中 xff0c 查阅资料后发现可以用下面的函数进行写入 span class token keyw
  • 最新zotero与obsidian笔记联动教程(可代替citations和mdnotes)

    最新zotero与obsidian笔记联动教程 xff08 可代替citations和mdnotes xff09 一 联动原理二 插件配置1 zotero better bibtex2 Bibnotes Formatter3 MarkDBC
  • 第十四周作业-必做2

    题目描述 xff1a Q老师 得到一张 n 行 m 列的网格图 xff0c 上面每一个格子要么是白色的要么是黑色的 Q老师认为失去了 十字叉 的网格图莫得灵魂 一个十字叉可以用一个数对 x 和 y 来表示 其中 1 x n 并且 1 y m
  • Ubuntu18.04系统及相关软件安装恢复过程

    Ubuntu18 04系统及相关软件安装恢复过程 一 常用软件安装1 系统安装 https blog csdn net qq 43309940 article details 116656810 2 显卡驱动安装 https blog cs
  • mujoco安装及urdf转xml方法记录

    参考 mujoco210及mujoco py安装 下载适用于Linux或 OSX的 MuJoCo 2 1 版二进制文件 将mujoco210的下载的目录解压到 mujoco mujoco210路径下 注意 xff1a 如果要为包指定非标准位
  • 多台虚拟机实现局域网内互连

    Step1 xff1a 要在同一局域网内连接上对方的虚拟机 xff0c 就要相应地使用VMware的桥接模式 xff0c 以桥接到这一局域网的网卡上 这里以我连接的学校的局域网为例 xff1a Step2 xff1a 打开VMware的虚拟
  • C++判断成绩等级

    else if span class token macro property span class token directive hash span span class token directive keyword include
  • C++判断一个数是不是质数

    详见及参考 xff1a https www nowcoder com practice b8bb5e7703da4a83ac7754c0f3d45a82 tpId 61 225 amp tags 61 amp title 61 amp di
  • C++比较字符串大小(自己实现strcmp()函数)

    详见 xff1a https www nowcoder com practice 963e455fdf7c4a4a997160abedc1951b tpId 61 225 amp tags 61 amp title 61 amp diffi
  • sql多表查询之合并查询(union)

    题1 https www nowcoder com practice 203d0aed8928429a8978185d9a03babc tpId 61 240 amp tags 61 amp title 61 amp difficulty