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查找当前打开的页数。

相关推荐
乘风御浪云帆之上42 分钟前
数据库操作【JDBC & HIbernate & Mybatis】
数据库·mybatis·jdbc·hibernate
AAA.建材批发刘哥1 小时前
Linux快速入门-Linux文件系统管理
linux·运维·服务器·c语言·学习方法
SomeB1oody1 小时前
【Rust自学】6.1. 定义枚举
开发语言·后端·rust
SomeB1oody1 小时前
【Rust自学】5.3. struct的方法(Method)
开发语言·后端·rust
LKAI.2 小时前
搭建Elastic search群集
linux·运维·elasticsearch·搜索引擎
dazhong20122 小时前
PLSQL 客户端连接 Oracle 数据库配置
数据库·oracle
啦啦右一3 小时前
Spring Boot | (一)Spring开发环境构建
spring boot·后端·spring
森屿Serien3 小时前
Spring Boot常用注解
java·spring boot·后端
青木沐3 小时前
Jenkins介绍
运维·jenkins
WTT00114 小时前
2024楚慧杯WP
大数据·运维·网络·安全·web安全·ctf