-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathqueries.sql
78 lines (70 loc) · 2.31 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
-- Получить список всех категорий
SELECT id, name FROM categories;
-- Получить список категорий для которых создано минимум одно объявление
SELECT id, name FROM categories
JOIN offer_categories
ON id = category_id
GROUP BY id;
-- Получить список категорий с количеством объявлений
SELECT id, name, count(offer_id) FROM categories
LEFT JOIN offer_categories
ON id = category_id
GROUP BY id;
-- Получить список объявлений
SELECT offers.*,
users.first_name,
users.last_name,
users.email,
COUNT(comments.id) AS comments_count,
STRING_AGG(DISTINCT categories.name, ', ') AS category_list
FROM offers
JOIN offer_categories ON offers.id = offer_categories.offer_id
JOIN categories ON offer_categories.category_id = categories.id
LEFT JOIN comments ON comments.offer_id = offers.id
JOIN users ON users.id = offers.user_id
GROUP BY offers.id, users.id
ORDER BY offers.created_at DESC;
-- Полная информация по объявлению
SELECT offers.*,
COUNT(comments.id) AS comments_count,
STRING_AGG(DISTINCT categories.name, ', ') AS category_list,
users.first_name,
users.last_name,
users.email
FROM offers
JOIN offer_categories ON offers.id = offer_categories.offer_id
JOIN categories ON offer_categories.category_id = categories.id
LEFT JOIN comments ON comments.offer_id = offers.id
JOIN users ON users.id = offers.user_id
WHERE offers.id = 1
GROUP BY offers.id, users.id;
-- Пять свежих комментариев
SELECT
comments.id,
comments.offer_id,
users.first_name,
users.last_name,
comments.text
FROM comments
JOIN users ON comments.user_id = users.id
ORDER BY comments.created_at DESC
LIMIT 5;
-- Все комментарии к объявлению
SELECT
comments.id,
comments.offer_id,
users.first_name,
users.last_name,
comments.text
FROM comments
JOIN users ON comments.user_id = users.id
WHERE comments.offer_id = 1
ORDER BY comments.created_at DESC;
-- Два объявления о покупке
SELECT * FROM offers
WHERE type = 'OFFER'
LIMIT 2;
-- Обновить заголовок
UPDATE offers
SET title = 'Уникальное предложение!'
WHERE id = 1;