gpt4 book ai didi

sql - SQL-2 中的 Twitter 风格的关注者表

转载 作者:搜寻专家 更新时间:2023-10-30 23:14:11 24 4
gpt4 key购买 nike

除此 Twitter style following-follower table in SQL

在上面的示例中,我能够返回“如果用户 B 正在关注其他关注他的用户”的查询

我现在需要一个更简单的东西。我试图修改原始代码但没有任何运气。我只需要“如果 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

我喜欢返回包含此关注者/关注状态的结果。例如对于用户 B(他关注 A 和 D 但不关注 C 和 E:

id      followedBy     areWeFollowing
1 userA 1
2 userB 0
3 userC 0
4 userD 1
5 userE 0

感谢您的帮助!

达达

最佳答案

对于这种情况,您甚至可以从第二张表中获取计数 -

select id,user_id, ( select count(*) 
from follows
where follows.user_id = users.user_id
and follows.follower_id = 'userB') as areWeFollowing
from users

关于sql - SQL-2 中的 Twitter 风格的关注者表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16677628/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com