关于后端:LeetCode-mysql-刷题三确认率MySQL-中的-null-处理-判断三角形的四种方法

5次阅读

共计 3698 个字符,预计需要花费 10 分钟才能阅读完成。

明天一题难度是中等,一题难度是简略

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

题目

题目链接:确认率

用户的 确认率 是 ‘confirmed’ 音讯的数量除以申请的确认音讯的总数。没有申请任何确认音讯的用户的确认率为 0。确认率四舍五入到 小数点后两位。

编写一个 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');
输出: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)
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
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

SELECT 1 / NULL; -- NULL
SELECT NULL / 2; -- NULL
SELECT 3 / 0; -- 谬误, 除数不能为 0
SELECT NULL / 0; -- NULL, 不会谬误
SELECT 3 / 1; -- 3

题目

题目链接:判断三角形

对每三个线段报告它们是否能够造成一个三角形。

以 任意程序 返回后果表。

查问后果格局如下所示。

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');
输出:
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 的对边
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 刷题二:电影评分——判断日期的五种办法
正文完
 0