MySQL实战1

文章目录

主要内容

  1. 墨西哥和美国第三高峰
  2. 用latest_event查找当前打开的页数

一.墨西哥和美国第三高峰

1.准备工作

代码如下(示例):
sql 复制代码
drop database if exists db_1;

create database db_1;

use db_1;

CREATE TABLE tb_mountains (
	name VARCHAR(50),
	height INT,
	country VARCHAR(50)
);

INSERT INTO tb_mountains (name, height, country)
VALUES
	('德纳利山', 20310, '美国'),
	('圣伊莱亚斯山', 18008, '美国'),
	('福拉克山', 17402, '美国'),
	('奥里萨巴峰', 18491, '墨西哥'),
	('波波卡特佩特尔火山', 17820, '墨西哥'),
	('伊斯塔西瓦特尔火山', 17160, '墨西哥')
;

select * from tb_mountains;

2.目标

  • 说明

  • 找出每个国家第三高的山名,并按ASC顺序对国家、地区排序

  • 输入

  • 输出

3.实现

代码如下(示例):
sql 复制代码
with t1 as (
	select
		name, height, country,
		dense_rank() over (partition by country order by height desc) as rn
	from tb_mountains
)
select
	name, height, country
from t1
where rn=3

4.相似例子

部门工资前三高的所有员工

代码如下(示例):
sql 复制代码
# todo 问题: 开窗函数中 1 row_number 2 rank 3 dense_rank 区别在什么地方
Create table If Not Exists Employee (id int, name varchar(255), salary int, depa
Create table If Not Exists Department (id int, name varchar(255));
Truncate table Employee;

insert into Employee (id, name, salary, departmentId) values ('1', 'Joe', '85000
insert into Employee (id, name, salary, departmentId) values ('2', 'Henry', '800
insert into Employee (id, name, salary, departmentId) values ('3', 'Sam', '60000
insert into Employee (id, name, salary, departmentId) values ('4', 'Max', '90000
insert into Employee (id, name, salary, departmentId) values ('5', 'Janet', '690
insert into Employee (id, name, salary, departmentId) values ('6', 'Randy', '850
insert into Employee (id, name, salary, departmentId) values ('7', 'Will', '7000
Truncate table Department;
insert into Department (id, name) values ('1', 'IT');
insert into Department (id, name) values ('2', 'Sales');


select * from employee;

select * from department;

with t1 as (
	select
		e.id, e.name employee, e.salary, e.departmentId, d.name as department
from Employee e
join department d on e.departmentId = d.id
),
t2 as (
	select
		id, employee, salary, departmentId, department,
		# row_number() over (partition by departmentId order by salary desc) as
		# rank() over (partition by departmentId order by salary desc) as rn
		dense_rank() over (partition by departmentId order by salary desc) as rn
	from t1
)
select
	department,
	employee,
	salary,
	rn
from t2
where rn<=3
;
with t1 as (
	select
		e.id, e.name employee, e.salary, e.departmentId, d.name as department
	from Employee e
	join department d on e.departmentId = d.id
),
t2 as (
	select
		id, employee, salary, departmentId, department,
		# row_number() over (partition by departmentId order by salary desc) as
		# rank() over (partition by departmentId order by salary desc) as rn
	dense_rank() over (partition by departmentId order by salary desc) as rn
from t1
)
select
	department,
	employee,
	salary
from t2
where rn<=3
;

二.用latest_event查找当前打开的页数

1.准备工作

代码如下(示例):
sql 复制代码
drop database if exists db_1;
create database db_1;
use db_1;

CREATE TABLE tb_pages_info (
	page_id INT,
	event_time TIMESTAMP,
	page_flag VARCHAR(3)
);
INSERT INTO tb_pages_info (page_id, event_time, page_flag)
VALUES
	(1, NOW() - INTERVAL 6 HOUR, 'ON'),
	(1, NOW() - INTERVAL 3 HOUR, 'OFF'),
	(1, NOW() - INTERVAL 1 HOUR, 'ON'),
	(2, NOW() - INTERVAL 3 HOUR, 'ON'),
	(2, NOW() - INTERVAL 1 HOUR, 'OFF'),
	(3, NOW(), 'ON');
	
select * from tb_pages_info;

2.目标

  • 说明

  • 查找当前正在使用的页面数,通过page_flag识别开/关

    根据题目描述,"tb_pages_info"表的结构如下所示:

    其中,"page_id"列表示⻚面的唯一标识符,"event_time"列表示⻚面状态更改的时间戳,"page_flag"列表示⻚面的状态标志,可以为"ON"或"OFF"。

    问题:给定下表,表中包含有关⻚面状态更改时间的信息。

    请使用SQL查找当前使用 latest_event 的⻚面数。

    注意,表中 page_flag 列将用于识别⻚面是『OFF』还是『ON』。

  • 输入

  • 输出

  • 解释

    根据给定的示例输入,页面2的最新状态为"OFF",页面1和页面3最新状态为"ON"。因此,具有最新状态的页面数量为2。

3.实现

代码如下(示例):
sql 复制代码
with t1 as (
	select
		page_id, event_time, page_flag,
		row_number() over (partition by page_id order by event_time desc) as rn
	from tb_pages_info
),
t2 as (
	select
		*
	from t1
	where rn=1 and page_flag='ON'
)
select
	count(1) as result
from t2
;

4.拓展问题

除了显示正在打开的页面数,还需要显示哪些页面?

代码如下(示例):
sql 复制代码
with t1 as (
	select
		page_id, event_time, page_flag,
		row_number() over (partition by page_id order by event_time desc) as
	from tb_pages_info
),
t2 as (
	select
		*
	from t1
	where rn=1 and page_flag='ON'
)
select
	count(1) over() as result,
	page_id
	# event_time, page_flag, rn
from t2
;

总结

以上是今天要讲的内容,实战了:墨西哥和美国第三高峰,用latest_event查找当前打开的页数。

相关推荐
三杯温开水几秒前
基于 CentOS7.6 的 Docker 下载常用的容器(MySQL&Redis&MongoDB),解决拉取容器镜像失败问题
redis·mysql·docker
微服务商城技术分享6 分钟前
通过Docker实现openGauss的快速容器化安装
运维·docker·容器
chusheng184012 分钟前
Python 正则表达式进阶用法:量词与范围
python·mysql·正则表达式
Desmend__13 分钟前
正则表达式那些事儿
数据库·mysql·正则表达式
袁庭新30 分钟前
LuaRocks如何安装数据库驱动?
java·数据库·redis·lua·luarocks·袁庭新
hummhumm38 分钟前
第 10 章 - Go语言字符串操作
java·后端·python·sql·算法·golang·database
Narutolxy41 分钟前
从 MySQL 5.7 到 8.0:理解 GROUP BY 的新规则与实战优化20241112
数据库
chusheng184042 分钟前
Python 正则表达式进阶用法:分组与引用详解
数据库·python·正则表达式
编程墨客1 小时前
第03章 文件编程
linux·运维·服务器
命里有定数1 小时前
windows工具 -- 使用rustdesk和云服务器自建远程桌面服务, 手机, PC, Mac, Linux远程桌面 (简洁明了)
linux·运维·服务器·windows·ubuntu·远程工作