LeetCode:Database 52.报告的记录

Posted Xiao Miao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode:Database 52.报告的记录相关的知识,希望对你有一定的参考价值。

要求:编写一条SQL,查询每种报告理由(report reason)在昨天的不同报告数量(post_id)。假设今天是 2019-07-05。

动作表:Actions的结构

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| user_id       | int     |
| post_id       | int     |
| action_date   | date    | 
| action        | enum    |
| extra         | varchar |
+---------------+---------+
此表没有主键,所以可能会有重复的行。
action 字段是 ENUM 类型的,包含:('view', 'like', 'reaction', 'comment', 'report', 'share')
extra 字段是可选的信息(可能为 null),其中的信息例如有:1.报告理由(a reason for report) 2.反应类型(a type of reaction)

Actions 表:

+---------+---------+-------------+--------+--------+
| user_id | post_id | action_date | action | extra  |
+---------+---------+-------------+--------+--------+
| 1       | 1       | 2019-07-01  | view   | null   |
| 1       | 1       | 2019-07-01  | like   | null   |
| 1       | 1       | 2019-07-01  | share  | null   |
| 2       | 4       | 2019-07-04  | view   | null   |
| 2       | 4       | 2019-07-04  | report | spam   |
| 3       | 4       | 2019-07-04  | view   | null   |
| 3       | 4       | 2019-07-04  | report | spam   |
| 4       | 3       | 2019-07-02  | view   | null   |
| 4       | 3       | 2019-07-02  | report | spam   |
| 5       | 2       | 2019-07-04  | view   | null   |
| 5       | 2       | 2019-07-04  | report | racism |
| 5       | 5       | 2019-07-04  | view   | null   |
| 5       | 5       | 2019-07-04  | report | racism |
+---------+---------+-------------+--------+--------+

Result Table:

+---------------+--------------+
| report_reason | report_count |
+---------------+--------------+
| spam          | 1            |
| racism        | 2            |
+---------------+--------------+ 
注意,我们只关心报告数量非零的结果。

SQL语句:

select extra as report_reason,count(distinct post_id) as report_count
from actions
where action_date=adddate('2019-07-05',-1) and extra is not null and action='report' 
group by extra;

以上是关于LeetCode:Database 52.报告的记录的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode:Database 35.学生地理信息报告

LeetCode:Database 71.报告系统状态的连续日期

LeetCode:Database 44.项目员工 III

LeetCode:Database 103.周内每天的销售情况

LeetCode:Database 103.周内每天的销售情况

LeetCode:Database 115.仓库经理