-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueries.sql
225 lines (185 loc) · 5.29 KB
/
queries.sql
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
-- Personal assessment of the most likely frequently used queries for FindIt.db
-- USER MANAGEMENT
-- Get user profile
SELECT
first_name,
last_name,
username,
birthdate,
bio,
profile_image_url,
banner_image_url
FROM users
WHERE id = ?;
-- Get user friends list
SELECT first_name, last_name, username, bio, profile_image_url
FROM users
WHERE id IN (
SELECT user_friend_id
FROM user_friends
WHERE user_id = ?
);
-- Check frienship status
SELECT `status`
FROM user_friends
WHERE user_id = ? AND user_friend_id = ?;
-- Update user bio
UPDATE users
SET bio = ?
WHERE id = ?;
-- END OF USER MANAGEMENT
-- COMMUNITY MANAGEMENT
-- Get community details
SELECT
title,
`description`,
bio,
is_verified,
business_phone,
business_email,
office_address,
events_location,
profile_image_url,
banner_image_url
FROM communities
WHERE id = ?;
-- Get communities the user has joined
SELECT title, bio, is_verified, events_location, profile_image_url, banner_image_url
FROM communities
WHERE id IN (SELECT community_id FROM user_joined_communities WHERE user_id = ?);
-- Get the Communities owned by the user
SELECT
communities.title,
communities.bio,
communities.is_verified,
communities.events_location,
user_own_communities.owned_at
FROM communities
JOIN user_own_communities ON communities.id = user_own_communities.community_id
WHERE user_own_communities.user_id = ?;
-- Get community post
SELECT
communities.title,
communities.profile_image_url,
community_posts.title,
community_posts.short_description,
community_posts.activity_location,
community_posts.created_at,
community_posts.updated_at,
post_images.image_url
FROM communities
JOIN community_posts ON communities.id = community_posts.community_id
JOIN post_images ON community_posts.id = post_images.post_id
WHERE community_posts.community_id = ?;
-- END OF COMMUNITY MANAGEMENT
-- POST INTERACTIONS
-- Get post comments
SELECT
users.first_name,
users.last_name,
users.profile_image_url,
user_post_comments.comment,
user_post_comments.commented_at
FROM users
JOIN user_post_comments ON users.id = user_post_comments.user_id
WHERE user_post_comments.post_id = ?;
-- Get post likes count
SELECT COUNT(*) AS like_count
FROM user_post_likes
WHERE post_id = ?;
-- Get post dislikes count
SELECT COUNT(*) AS dislike_count
FROM user_post_dislikes
WHERE post_id = ?;
-- Get users interested in post
SELECT COUNT(*) AS post_interest
FROM user_post_interest
WHERE user_post_interest.post_id = ?;
-- Get users interested in post
SELECT
users.first_name,
users.last_name,
users.bio,
users.profile_image_url,
user_post_interest.interested_at
FROM users
JOIN user_post_interest ON users.id = user_post_interest.user_id
WHERE user_post_interest.post_id = ?;
-- Add a like to post
INSERT INTO user_post_likes (user_id, post_id)
VALUES (?, ?);
-- Add a dislike to a post
INSERT INTO user_post_dislikes (user_id, post_id)
VALUES (?, ?);
-- END OF POST INTERACTIONS
-- POST CREATION AND CONTENT MANAGEMENT
-- Create a new post
INSERT INTO community_posts (community_id, title, short_description, activity_location)
VALUES (?, ?, ?, ?);
-- Add images to post
INSERT INTO post_images (post_id, image_url)
VALUES (?, ?);
-- Add categories to a post
INSERT INTO post_categories (post_id, category_id)
VALUES (?, ?);
-- Add tags to a post
INSERT INTO post_tags (post_id, tag_id)
VALUES (?, ?);
-- END OF POST CREATION AND CONTENT MANAGEMENT
-- COMMUNITY RATING AND REVIEW
-- Get community rating by user
SELECT
users.first_name,
users.last_name,
users.profile_image_url,
user_community_rating.rating,
user_community_rating.comment,
user_community_rating.rated_at
FROM users
JOIN user_community_rating ON users.id = user_community_rating.user_id
WHERE user_community_rating.user_id = ? AND user_community_rating.community_id = ?;
-- Get average community rating
SELECT ROUND(AVG(rating), 2) AS average_rating
FROM user_community_rating
WHERE community_id = ?;
-- END OF COMMUNITY RATING AND REVIEW
-- SEARCH FUNCTIONALITY
-- Search posts by category
SELECT
community_posts.title,
community_posts.short_description,
community_posts.activity_location,
community_posts.created_at,
post_images.image_url
FROM community_posts
JOIN post_images ON community_posts.id = post_images.post_id
JOIN post_categories ON community_posts.id = post_categories.post_id
JOIN categories ON categories.id = post_categories.category_id
WHERE categories.`type` IN (?);
-- Search posts by title
SELECT
community_posts.title,
community_posts.short_description,
community_posts.activity_location,
community_posts.created_at,
post_images.image_url
FROM community_posts
JOIN post_images ON community_posts.id = post_images.post_id
WHERE community_posts.title LIKE ?;
-- Search user by first and last name
SELECT first_name, last_name, bio, profile_image_url
FROM users
WHERE first_name LIKE ? AND last_name LIKE ?;
-- END OF SEACH FUNCTIONALITY
-- CATEGORY AND SUBCATEGORY MANAGEMENT
-- Get all categories
SELECT `type` FROM categories;
-- Get all subcategories for a category
SELECT `type`
FROM subcategories
WHERE id IN (
SELECT subcategory_id
FROM category_subcategories
WHERE category_id = ?
);
-- END OF CATEGORY AND SUBCATEGORY MANAGEMENT