-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1148.article-views-i.sql
More file actions
122 lines (117 loc) · 3.49 KB
/
1148.article-views-i.sql
File metadata and controls
122 lines (117 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#
# @lc app=leetcode id=1148 lang=mysql
#
# [1148] Article Views I
#
# Difficulty: Easy
# Tags: Database
# URL: https://leetcode.com/problems/article-views-i/
#
#
# --- Problem Description ---
#
# Table: Views
#
# +---------------+---------+
# | Column Name | Type |
# +---------------+---------+
# | article_id | int |
# | author_id | int |
# | viewer_id | int |
# | view_date | date |
# +---------------+---------+
# There is no primary key (column with unique values) for this table, the
# table may have duplicate rows.
# Each row of this table indicates that some viewer viewed an article
# (written by some author) on some date.
# Note that equal author_id and viewer_id indicate the same person.
#
#
#
# Write a solution to find all the authors that viewed at least one of
# their own articles.
#
# Return the result table sorted by id in ascending order.
#
# The result format is in the following example.
#
#
#
# Example 1:
#
# Input:
# Views table:
# +------------+-----------+-----------+------------+
# | article_id | author_id | viewer_id | view_date |
# +------------+-----------+-----------+------------+
# | 1 | 3 | 5 | 2019-08-01 |
# | 1 | 3 | 6 | 2019-08-02 |
# | 2 | 7 | 7 | 2019-08-01 |
# | 2 | 7 | 6 | 2019-08-02 |
# | 4 | 7 | 1 | 2019-07-22 |
# | 3 | 4 | 4 | 2019-07-21 |
# | 3 | 4 | 4 | 2019-07-21 |
# +------------+-----------+-----------+------------+
# Output:
# +------+
# | id |
# +------+
# | 4 |
# | 7 |
# +------+
#
#
# --- Community Solutions ---
#
# https://leetcode.com/problems/article-views-i/solutions
#
# @lc code=start
# SQL / 基础查询:筛自读记录 + 去重排序
#
# 这题其实在做什么:
# Views 表记录谁看了哪篇文章。找出看过自己文章的作者 id。
# 如果同一个作者多次看自己的文章,只输出一次。
#
# 识别信号:
# - 同一行里比较 author_id 和 viewer_id → 单表 WHERE。
# - “viewed at least one of their own articles” → author_id = viewer_id。
# - “return sorted by id” + “每个作者一次” → DISTINCT + ORDER BY。
#
# 核心思路:
# 先筛出自读记录:`author_id = viewer_id`。
# 然后把 author_id 改名成 id 输出,并用 DISTINCT 去重。
# 最后按 id 升序排列。
#
# 查询形状:
# SELECT DISTINCT author_id AS id
# FROM Views
# WHERE author_id = viewer_id
# ORDER BY id
#
# Join / Filter / Grouping:
# - Join:不需要,作者和浏览者都在同一行。
# - Filter:同一行内两个字段相等。
# - Grouping:可用 DISTINCT 去重;不需要 GROUP BY 统计。
#
# 边界条件 / 易错点:
# - 不去重会把同一个作者重复输出多次。
# - 输出列名必须是 id,不是 author_id。
# - 排序按输出 id 升序;不要按 article_id 或 view_date。
#
# 落码步骤 / TODO:
# 1. WHERE author_id = viewer_id。
# 2. SELECT DISTINCT author_id AS id。
# 3. ORDER BY id。
#
# 复杂度:
# 逻辑上扫描 Views,筛选后去重排序;真实成本取决于数据量和索引。
#
# 一句模板:
# 同一行字段互相比较用 WHERE;“每人一次”用 DISTINCT。
# DISTINCT 去重,保证每个作者只出现一次
SELECT DISTINCT author_id AS id
FROM Views
# 同一行里作者 == 浏览者,说明作者看了自己的文章
WHERE author_id = viewer_id
ORDER BY id;
# @lc code=end