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

Mysql刷题 day07

LC 1907 按分类统计薪水

知识点:

1.select + 字面量可以直接返回该字面量。平时都是从表中取数据 ,针对这个点还真不太熟悉。

比如 select 'Low Salary'  as Salary 相当于直接返回 'Low Salary'这个字符串 , 并给这个字段取名叫salary。

2.本体思路就是把各个类别的数据集都选出来 , 在union一下。

select 'Low Salary' as category , count(case when income < 20000 then 1 end) as accounts_count from Accounts
union all
select 'Average Salary' as category , count(case when income >=20000 and income <= 50000 then 1 end) as accounts_count from Accounts 
union all 
select 'High Salary' as category , count(case when income > 50000 then 1 end) as accounts_count from Accounts

LC 1164 指定日期的产品价格

思路

0.先把每个产品的id单独选出来

1.找到每个产品在'2019-08-16' 前的最后一次变更日期

这两步可以用公共表达式先选出来。

关于多个CTE的语法:

WITHtemp AS (...),temp2 AS (...)
SELECT ...
with temp as(
select product_id , max(change_date) as latest from Products where change_date <= '2019-08-16' group by product_id
)
,
distinct_id as(select distinct product_id from Products
)

2.关联所有产品id和对应的最后变化时间 ,第一次left join

3.选出价格 ,并且早于8-16的设置为默认值 , 第二次left join

因此,本体代码如下:

with temp as(
select product_id , max(change_date) as latest from Products where change_date <= '2019-08-16' group by product_id
)
,
distinct_id as(select distinct product_id from Products
)
select distinct_id.product_id , 
case 
when new_price is not null then new_price else 10
end as price
from distinct_id left join temp on distinct_id.product_id = temp.product_id
left join Products p on p.product_id = temp.product_id and temp.latest = p.change_date

LC 626 换座位

知识点:关联子查询

何为关联子查询?

关联子查询(Correlated Subquery)是指 ​​子查询依赖于外层查询的字段值​​,执行流程为:

  1. ​外层查询逐行扫描​​:每次取一行数据。
  2. ​子查询动态执行​​:根据外层查询的当前行值,返回相关结果。
  3. ​结果合并​​:将子查询结果与外层查询的行合并。

语法:

SELECT 外层查询字段,(SELECT 子查询字段 FROM 子查询表 WHERE 子查询表.字段 = 外层表别名.字段  -- 🟢 关联条件) AS 别名
FROM 外层表 AS 外层表别名  -- 🟢 外层查询别名
WHERE 外层查询条件;

注意 ,关联子查询时 , 外层表必须取别名!

本体代码:


select
s.id ,
case
when s.id % 2 = 1 and s.id != (select count(1) from Seat) then (select student from Seat where id = s.id + 1) 
when s.id % 2 = 1 and s.id = (select count(1) from Seat) then student
when s.id % 2 = 0  then (select student from Seat where id = s.id - 1)
end as student 
from Seat s

其中的

 (select student from Seat where id = s.id + 1) 
when s.id % 2 = 0  then (select student from Seat where id = s.id - 1)

就是关联子查询。id = s.id - 1就是关联条件

具体执行流程如下:

1.外层先执行 

2.没执行一行 , 就会触发子查询。

比如上面 ,当外层查到s.id = 1时,正好满足第一个case ,进入内层子查询,内层子查询是在Seat表(注意没起别名!)中查 ,id = s.id + 1 = 1 + 1. 即查到了第二行的名字,通过这样的逻辑完成交换。

http://www.xdnf.cn/news/7692.html

相关文章:

  • 苍穹外卖系统结构与功能报告
  • 飞致云旗下开源项目GitHub Star总数突破150,000个
  • 集成运算放大器知识汇总
  • js如何复制图片
  • 嘉立创EDA成图:原理图绘制以及PCB封装导出为.efoo文件
  • 用于管理共享内存的 C# 类 ShareMemory
  • Python 训练营打卡 Day 30
  • SpringBoot实现本地对象存储【minio、阿里云、七牛云】
  • Python-多进程编程 (multiprocessing 模块)
  • 101个α因子#6
  • P2670 [NOIP 2015 普及组] 扫雷游戏
  • 使用VGG-16模型来对海贼王中的角色进行图像分类
  • 【CodeBuddy 】从0到1,打造一个“牛马打鸡血仪”
  • C++ 初阶 | 类和对象易错知识点(上)
  • leetcode2310. 个位数字为 K 的整数之和-medium
  • Python字符串切片详解
  • Oracle中如何解决FREE BUFFER WAITS
  • Modbus通信协议详解
  • 字典和哈希表(javascript版)
  • 水利数据采集MCU水资源的智能守护者
  • 使用VMWare安装的ubuntu虚拟机,突然无法上网.
  • 鸿蒙应用开发:应用运行到设备报错,可能是版本问题,可通过hdc查询设备API版本
  • 第8天-Python趣味绘图:用Turtle库开启绘画编程之旅
  • tcpdump抓包
  • Nuxt的SEO实践
  • 基于MakeReal3D的虚拟预装系统:飞机装配效率与精度的双重突破
  • 告别蜘蛛池!PHP 打造你的网站专属蜘蛛导航仪
  • UDP协议简介
  • Runtime Suspend 专项训练
  • Apollo10.0学习——planning模块(8)之scenario、Stage插件详解二