LeetCode mysql 刷题三:确认率——MySQL 中的 null 处理 | 判断三角形的四种方法

今天一题难度是中等,一题难度是简单

第一题确认率考察的知识点是:null 值处理,第二题考察的是如何判断三角形

题目

题目链接:确认率

用户的 确认率 是 'confirmed' 消息的数量除以请求的确认消息的总数。没有请求任何确认消息的用户的确认率为 0 。确认率四舍五入到 小数点后两位 。

编写一个 SQL 查询来查找每个用户的 确认率 。

以 任意顺序 返回结果表。

查询结果格式如下所示。

sql 复制代码
Create table If Not Exists Signups (user_id int, time_stamp datetime);
Create table If Not Exists Confirmations (user_id int, time_stamp datetime, action ENUM('confirmed','timeout'));
Truncate table Signups;
insert into Signups (user_id, time_stamp) values ('3', '2020-03-21 10:16:13');
insert into Signups (user_id, time_stamp) values ('7', '2020-01-04 13:57:59');
insert into Signups (user_id, time_stamp) values ('2', '2020-07-29 23:09:44');
insert into Signups (user_id, time_stamp) values ('6', '2020-12-09 10:39:37');
Truncate table Confirmations;
insert into Confirmations (user_id, time_stamp, action) values ('3', '2021-01-06 03:30:46', 'timeout');
insert into Confirmations (user_id, time_stamp, action) values ('3', '2021-07-14 14:00:00', 'timeout');
insert into Confirmations (user_id, time_stamp, action) values ('7', '2021-06-12 11:57:29', 'confirmed');
insert into Confirmations (user_id, time_stamp, action) values ('7', '2021-06-13 12:58:28', 'confirmed');
insert into Confirmations (user_id, time_stamp, action) values ('7', '2021-06-14 13:59:27', 'confirmed');
insert into Confirmations (user_id, time_stamp, action) values ('2', '2021-01-22 00:00:00', 'confirmed');
insert into Confirmations (user_id, time_stamp, action) values ('2', '2021-02-28 23:59:59', 'timeout');
lua 复制代码
输入:
Signups 表:
+---------+---------------------+
| user_id | time_stamp          |
+---------+---------------------+
| 3       | 2020-03-21 10:16:13 |
| 7       | 2020-01-04 13:57:59 |
| 2       | 2020-07-29 23:09:44 |
| 6       | 2020-12-09 10:39:37 |
+---------+---------------------+
User_id是该表的主键。
每一行都包含ID为user_id的用户的注册时间信息。

Confirmations 表:
+---------+---------------------+-----------+
| user_id | time_stamp          | action    |
+---------+---------------------+-----------+
| 3       | 2021-01-06 03:30:46 | timeout   |
| 3       | 2021-07-14 14:00:00 | timeout   |
| 7       | 2021-06-12 11:57:29 | confirmed |
| 7       | 2021-06-13 12:58:28 | confirmed |
| 7       | 2021-06-14 13:59:27 | confirmed |
| 2       | 2021-01-22 00:00:00 | confirmed |
| 2       | 2021-02-28 23:59:59 | timeout   |
+---------+---------------------+-----------+
(user_id, time_stamp)是该表的主键。
user_id是一个引用到注册表的外键。
action是类型为('confirmed', 'timeout')的ENUM
该表的每一行都表示ID为user_id的用户在time_stamp请求了一条确认消息,该确认消息要么被确认('confirmed'),要么被过期('timeout')。

输出:
+---------+-------------------+
| user_id | confirmation_rate |
+---------+-------------------+
| 6       | 0.00              |
| 3       | 0.00              |
| 7       | 1.00              |
| 2       | 0.50              |
+---------+-------------------+
解释:
用户 6 没有请求任何确认消息。确认率为 0。
用户 3 进行了 2 次请求,都超时了。确认率为 0。
用户 7 提出了 3 个请求,所有请求都得到了确认。确认率为 1。
用户 2 做了 2 个请求,其中一个被确认,另一个超时。确认率为 1 / 2 = 0.5。

解析

此题考点是 null 值处理

null 处理:

  • sum(column)count(column) 都会忽略 null
  • 除数或者被除数为 null 时,结果为 null

如果遇到 null 值,我们需要将 null 转为 0

  • if(condition , 1, 0)
  • ifnull(condition, 0)
sql 复制代码
SELECT
  user_id,
  ifnull(round(sum( action = "confirmed" ) / count( user_id ), 2), 0.00) confirmation_rate
FROM Signups LEFT JOIN Confirmations USING ( user_id )
GROUP BY user_id
sql 复制代码
SELECT
  user_id,
  round(if(sum( action = "confirmed" ), 1, 0) / count( user_id ), 2) confirmation_rate
FROM Signups LEFT JOIN Confirmations USING ( user_id )
GROUP BY	user_id

除数和被除数为 null 的结果

只要表达式中包含 null 结果就是 null

sql 复制代码
SELECT 1 / NULL; -- NULL
SELECT NULL / 2; -- NULL
SELECT 3 / 0; -- 错误,除数不能为0
SELECT NULL / 0; -- NULL,不会错误
SELECT 3 / 1; -- 3

题目

题目链接:判断三角形

对每三个线段报告它们是否可以形成一个三角形。

以 任意顺序 返回结果表。

查询结果格式如下所示。

sql 复制代码
Create table If Not Exists Triangle (x int, y int, z int);
Truncate table Triangle;
insert into Triangle (x, y, z) values ('13', '15', '30');
insert into Triangle (x, y, z) values ('10', '20', '15');
sql 复制代码
输入:
Triangle 表:
+----+----+----+
| x  | y  | z  |
+----+----+----+
| 13 | 15 | 30 |
| 10 | 20 | 15 |
+----+----+----+
在 SQL 中,(x, y, z)是该表的主键列。
该表的每一行包含三个线段的长度。

输出:
+----+----+----+----------+
| x  | y  | z  | triangle |
+----+----+----+----------+
| 13 | 15 | 30 | No       |
| 10 | 20 | 15 | Yes      |
+----+----+----+----------+

解析

判断三角形的 4 种方法:

  1. 任意两边之和大于等于第三边
  2. 任意两边之差小于等于第三边
  3. 三边之和大于最大边的 2 倍
  4. 余弦定理
    • z² = x² + y² - 2xy * cosC
    • Cxy 的夹角,zC 的对边
sql 复制代码
SELECT
	x,
	y,
	z,
CASE
  WHEN x - y < z AND x - z < y AND z - y < x THEN "Yes" ELSE "No"
END triangle
FROM Triangle

往期 MySQL 题目

  1. MySQL 题目
  2. LeetCode mysql 刷题一:计算特殊奖金 | 买下所有产品的客户
  3. LeetCode mysql 刷题二:电影评分------判断日期的五种方法
相关推荐
heartbeat..19 分钟前
Redis 常用命令全解析:基础、进阶与场景化实战
java·数据库·redis·缓存
数据知道25 分钟前
PostgreSQL 实战:一文掌握如何优雅的进行递归查询?
大数据·数据库·postgresql
陌上丨29 分钟前
MySQL8.0高可用集群架构实战
数据库·mysql·架构
重生之绝世牛码40 分钟前
Linux软件安装 —— ClickHouse单节点安装(rpm安装、tar安装两种安装方式)
大数据·linux·运维·数据库·clickhouse·软件安装·clickhouse单节点
一只自律的鸡1 小时前
【MySQL】第十一章 存储过程和存储函数
数据库·mysql
翔云1234561 小时前
MySQL 中的 utf8 vs utf8mb4 区别
数据库·mysql
AIFQuant1 小时前
如何通过股票数据 API 计算 RSI、MACD 与移动平均线MA
大数据·后端·python·金融·restful
数据知道1 小时前
PostgreSQL 实战:索引的设计原则详解
数据库·postgresql
x70x801 小时前
Go中nil的使用
开发语言·后端·golang
强子感冒了1 小时前
MySQL学习随笔:数据类型与字段约束
学习·mysql