Codewise
Courses
My learning
Ideas
Leaderboard
YouTube
Courses
My learning
Ideas
Leaderboard
YouTube
Sign in
db/migrations/0003_followers_count.up.sql · System Design in Go: Build a Social Network Backend | Codewise
System Design in Go: Build a Social Network Backend
murmur
/
db
/
migrations
/
0003_followers_count.up.sql
main
0003_followers_count.up.sql
7 lines, 296 B
Raw
1
alter
table
users
add
column followers_count
integer
not null
default
0
;
2
3
-- Users who already have followers get their count from the follows table.
4
update
users u
5
set
followers_count
=
c
.
n
6
from
(
select
followee_id,
count
(
*
)
as
n
from
follows
group by
followee_id) c
7
where
u
.
id
=
c
.
followee_id
;