当前位置: 首页 > news >正文

MySql性能调优05-[sql实战演练]

sql实战演练

  • 行列转换
    • 行列式转换
      • 第一题【列转行】
      • 第二题【列转行】
  • having的使用
    • 找到表中,名字重复的项
    • 有数据表employee,包含如下字段id、name、department、age,编写SQL,找到不与其他人同龄的年纪最大的员工的年龄
    • 有数据表employee,包含如下字段id、name、department、age,编写SQL,找到与其他人同龄的年纪最大的员工的年龄

行列转换

行列式转换

第一题【列转行】

在这里插入图片描述

  • A结果答案
select t.name 学生Name,c.不及格,c.及格 From stu t inner join (select b.no, sum(case when b.result<60 then 1 else 0 end) 不及格,sum(case when b.result>=60 then 1 else 0 end ) 及格 From res b group by b.no) c on t.no=c.no;
  • B结果答案
select t.name 学生Name,c.数学,c.语文,c.英语 From stu t inner join (select b.no,sum(case when b.course='数学' then b.result else 0 end) 数学,sum(case when b.course='语文' then b.result else 0 end) 语文,sum(case when b.course='英语' then b.result else 0 end) 英语From res b group by b.no) c on t.no=c.no;

第二题【列转行】

在这里插入图片描述

  • 答案
select a.zh,sum(case when a.date='202301' then a.amt else 0 end) a,sum(case when a.date='202302' then a.amt else 0 end) b,sum(case when a.date='202303' then a.amt else 0 end) cFrom account a group by a.zh;

having的使用

找到表中,名字重复的项

select t.name From person t group by t.name having count(*)>1;

有数据表employee,包含如下字段id、name、department、age,编写SQL,找到不与其他人同龄的年纪最大的员工的年龄

select age from employees where age not in(select age from employees group by age having count(*)>1) order by age desc limit 0,1;

有数据表employee,包含如下字段id、name、department、age,编写SQL,找到与其他人同龄的年纪最大的员工的年龄

select max(t.age) from (select age from employees group by age having count(*)>1) t;
http://www.lryc.cn/news/405990.html

相关文章:

  • go-kratos 学习笔记(1) 安装
  • 蚂蚁集团推出EchoMimic:能通过音频和面部标志生成逼真的肖像动画视频
  • 量化机器人对市场趋势的反应速度
  • 深入指南:VitePress 如何自定义样式
  • 逃离的日常:自闭症孩子的课堂小插曲
  • LLM模型与实践之基于MindSpore的GPT2文本摘要
  • 【Android】使用视图绑定ViewBinding来代替findViewById
  • 字符的统计——423、657、551、696、467、535
  • pytest+allure
  • 【数据结构】AVL树(平衡二叉搜索树)
  • ASP.NET Web Api 使用 EF 6,DateTime 字段如何取数据库服务器当前时间
  • 【HarmonyOS】应用设置屏幕常亮
  • Docker部署Elasticsearch8.6.0 Kibana8.6.0
  • 第四篇论文小记
  • python使用 tkinter 生成随机颜色
  • 【Linux学习 | 第1篇】Linux介绍+安装
  • 设计模式-抽象工厂
  • Ubunton-24.04 简单配置使用
  • 什么是STP环路保护
  • Python算法基础:解锁冒泡排序与选择排序的奥秘
  • QtCMake工程提升类后找不到头文件
  • Docker核心技术:Docker原理之Cgroups
  • union的特性和大小端
  • 个性化IT服务探索实践
  • UE4-打包游戏,游戏模式,默认关卡
  • Unity ShaderLab基础
  • 用代理IP会频繁掉线是什么原因?HTTP和SOCKS5协议优劣势是什么?
  • MongoDB教程(十三):MongoDB覆盖索引
  • 快速认识EA(Enterprise Architecture)
  • 词云图制作