-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviews.sql
More file actions
67 lines (57 loc) · 1.5 KB
/
views.sql
File metadata and controls
67 lines (57 loc) · 1.5 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
use blog_site;
-- *****************************************************************************************
-- view for posts with all info
-- drop view postWithCategories;
create or replace view postWithCategories as
select
p.post_id,
p.title,
p.content,
p.user_id,
u.name as user_name,
p.post_date,
p.no_of_likes,
group_concat(c.category_name separator ', ') as category_names
from
post p
join
user u on p.user_id = u.user_id
left join
post_category pc on p.post_id = pc.post_id
left join
category c on pc.category_id = c.category_id
group by
p.post_id;
select * from postwithcategories;
-- *****************************************************************************************
-- view for posts with above average likes
create view postsAboveAverageLikes as
select
p.post_id,
p.title,
p.content,
u.name as user_name,
p.no_of_likes
from
post p
join
user u on p.user_id = u.user_id
where
p.no_of_likes > (select avg(no_of_likes) from post);
select * from postsaboveaveragelikes;
-- *****************************************************************************************
select * from comment;
create view postComments as
select
post.post_id,
post.title as post_title,
comment.comment_id,
comment.comment_content,
user.user_id
from
comment
join
post on comment.post_id = post.post_id
left join
user on comment.user_id = user.user_id;
select * from postComments where post_id = 24;