SQL中Twitter样式的follow -follower表



我在mySQL中有一个基本的follower/following表,看起来像这样。

id      user_id     follower_id
1           userA       userB
2           userC       userA
3           userA       userC
4           userB       userC
5           userB       userA

我检查了这些主题,但没有找到我需要的

数据库设计'followers'和& # 39;追随者# 39;?

SQL Following and follower

我需要的是这样一个系统:

假设我们是userB(后面跟着userA,后面跟着userC和userA)

我想返回一个包含这个follower/following状态的结果。例如userB:

id      followedBy     areWeFollowing
1           userA       1
2           userC       0

谢谢你的帮助!

斯巴达

用这个查询来查找如果你关注谁,那么也关注你。

SELECT ff1.follower_id as followedBy,
(
   select count(follower_id) 
   from follower_following as ff2 
   where ff2.user_id = ff1.follower_id 
   and ff2.follower_id = ff1.user_id 
) as areWeFollowing 
FROM follower_following as ff1  
where user_id = 'userB';

最新更新