Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

53 文章浏览 Ⅰ #65

Open
astak16 opened this issue Mar 25, 2022 · 0 comments
Open

53 文章浏览 Ⅰ #65

astak16 opened this issue Mar 25, 2022 · 0 comments
Labels

Comments

@astak16
Copy link
Owner

astak16 commented Mar 25, 2022

题目

题目链接:文章浏览 Ⅰ

此表的每一行都表示某人在某天浏览了某位作者的某篇文章。

请注意,同一人的 author_idviewer_id 是相同的。

编写一条 SQL 查询以找出所有浏览过自己文章的作者,结果按照 author_id 升序排列。

create table  views (
  article_id int, 
  author_id int, 
  viewer_id int, 
  view_date date
);

insert into views (article_id, author_id, viewer_id, view_date) values 
(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');

SQL:方法一

select distinct author_id id from views where author_id = viewer_id order by id;

解析

因为同一个人的 author_idviewer_id 是相同的,所以直接筛选 author_id = viewer_id ,就能找出浏览过自己文章的作者了。

@astak16 astak16 added the 简单 label Mar 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant