-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.sql
41 lines (34 loc) · 1001 Bytes
/
test.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
-- delete from friendships;
-- DELETE FROM users;
-- SELECT
-- users.id,
-- username,
-- count(friendships.user_id) as 'following_count',
-- (SELECT count(*)
-- FROM friendships
-- WHERE friendships.friend_id = users.id) as 'followers_count'
-- FROM users
-- JOIN friendships ON friendships.user_id = users.id
-- GROUP BY users.id;
-- select id, username from users order by random() limit 2;
-- USERS:
-- id, username
-- FRIENDSHIPS
-- id, user_id, friend_id
--select users.username from users join friendships on friend_id=users.id where user_id=14 order by username;
SELECT users.username
FROM users
JOIN friendships ON friend_id = users.id
WHERE user_id = 14
INTERSECT
SELECT users.username
FROM users
JOIN friendships ON friend_id = users.id
WHERE user_id = 12;
-- (User.find(12).friends & User.find(14).friends).count
SELECT
f1.username as 'source',
f2.username as 'target'
FROM friendships
JOIN users f1 ON user_id = f1.id
JOIN users f2 ON friend_id = f2.id;