简体   繁体   English

SQL中的Twitter样式跟随者表

[英]Twitter style following-follower table in SQL

I have a basic follower/following table in mySQL which looks like this. 我在mySQL中有一个基本的关注者/关注表,看起来像这样。

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

I checked these topics but couldnt find what I need 我检查了这些主题,但无法找到我需要的东西

database design for 'followers' and 'followings'? “追随者”和“追随者”的数据库设计?

SQL Following and Followers SQL关注者和关注者

What I need is to have a system like this: 我需要的是拥有这样一个系统:

Assume we are userB ( we follow userA, we are followed by userC and userA) 假设我们是userB(我们关注userA,接下来是userC和userA)

I like to return a result that includes this follower/following state. 我想返回包含此关注者/关注状态的结果。 For example for userB: 例如对于userB:

id      followedBy     areWeFollowing
1           userA       1
2           userC       0

Thanks for your help! 谢谢你的帮助!

arda 阿尔达

With this query to find if who you follow, follow you too. 通过此查询查找您关注的人,也可以关注您。

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';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM