添加到此Twitter style following-follower table in SQL
在上面的示例中,我能够返回"if userB正在关注他的其他用户“的查询。
我现在需要一个更简单的东西。我试着修改了原始代码,但没有任何运气。我只需要“如果userB正在关注任何用户,不管他们是否关注他。目前它为不关注该特定用户的人返回null。
这是针对用户的示例表
id user_id
1 userA
2 userB
3 userC
4 userD
5 userE这是下面的示例表
id user_id follower_id
1 userA userB
2 userD userB我喜欢返回一个包含这个关注者/关注状态的结果。例如userB (他遵循A和D,但不遵循C和E:
id followedBy areWeFollowing
1 userA 1
2 userB 0
3 userC 0
4 userD 1
5 userE 0谢谢你的帮忙!
阿尔达
发布于 2013-05-22 03:26:52
对于这种情况,您甚至可以从第二个表中进行计数-
select id,user_id, ( select count(*)
from follows
where follows.user_id = users.user_id
and follows.follower_id = 'userB') as areWeFollowing
from usershttps://stackoverflow.com/questions/16677628
复制相似问题