forked from htmlacademy-nodejs/144889-typoteka-3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
queries.sql
79 lines (72 loc) · 2.5 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
/*Список всех категорий*/
SELECT * FROM categories;
/*Список непустых категорий*/
SELECT id, name
FROM categories
JOIN articles_categories ON id = category_id
GROUP BY id;
/*Категории с количеством публикаций*/
SELECT id, name, count(article_id)
FROM categories
JOIN articles_categories ON id = category_id
GROUP BY id
ORDER BY count(article_id) DESC;
/*Список публикаций, сначала свежие*/
SELECT
articles.id,
articles.title,
articles.announce,
articles.created_at,
concat(users.first_name, ' ', users.last_name) AS author_name,
users.email AS author_email,
count(comments.id) AS comments_count,
string_agg(DISTINCT categories.name, ', ') AS article_categories
FROM articles
JOIN users ON articles.user_id = users.id
LEFT JOIN comments ON articles.id = comments.article_id
LEFT JOIN articles_categories ON articles.id = articles_categories.article_id
LEFT JOIN categories ON articles_categories.category_id = categories.id
GROUP BY articles.id, users.id
ORDER BY articles.created_at DESC;
/*Детальная информация по публикации*/
SELECT
articles.id,
articles.title,
articles.announce,
articles.fullText,
articles.created_at,
concat(users.first_name, ' ', users.last_name) AS author_name,
users.email AS author_email,
count(comments.id) AS comments_count,
string_agg(DISTINCT categories.name, ', ') AS article_categories
FROM articles
JOIN users ON articles.user_id = users.id
LEFT JOIN comments ON articles.id = comments.article_id
LEFT JOIN articles_categories ON articles.id = articles_categories.article_id
LEFT JOIN categories ON articles_categories.category_id = categories.id
WHERE articles.id = 1
GROUP BY articles.id, users.id;
/*Пять свежих комментариев*/
SELECT
comments.id,
comments.article_id,
concat(users.first_name, ' ', users.last_name) AS author,
comments.text
FROM comments
JOIN users ON comments.user_id = users.id
ORDER BY comments.created_at DESC
LIMIT 5;
/*Комментарии к публикации*/
SELECT
comments.id,
comments.article_id,
concat(users.first_name, ' ', users.last_name) AS author,
comments.text
FROM comments
JOIN users ON comments.user_id = users.id
WHERE comments.article_id = 1
ORDER BY comments.created_at DESC;
/*Обновить заголовок публикации*/
UPDATE articles
set title = 'Ёлки. История деревьев'
WHERE id = 1;