关注我在SQL中关注的用户

发布于 2025-01-23 18:35:56 字数 460 浏览 0 评论 0原文

的表

id | name
---------
1  | John
2  | Mark
3  | Bill
4  | Steve
5  | Jeff

我有两个名为用户fans 代码>表:

fan | user
1   | 2
1   | 5
2   | 3
2   | 4
2   | 5

“ fan”是关注用户的用户,“用户”是正在关注的用户。现在,如果我们将约翰(ID 1)作为当前用户,并且鉴于John正在关注Mark(ID 2),那么我们如何获得约翰所关注的每个人的以下内容,而不是约翰已经关注的人(在John遵循Mark遵循用户3、4和5的标记,但由于John已经关注用户5,只能返回3和4。我尝试使用运算符中的进行此操作,但我不确定它会随着更多条目而缩小。任何帮助将不胜感激。

I have two tables called users and fans.
The users table looks like this:

id | name
---------
1  | John
2  | Mark
3  | Bill
4  | Steve
5  | Jeff

And this is the fans table:

fan | user
1   | 2
1   | 5
2   | 3
2   | 4
2   | 5

"Fan" is the user who is following the user and "user" is the one who is being followed. Now If we take John (id 1) as the current user, and given that John is following Mark (id 2), How can we get the following of everyone whom John is following, but not the ones whom John is already following (In this case John is following Mark. Mark is following user 3, 4 and 5 but only 3 and 4 should be returned because John is already following user 5.)? I tried doing this with the IN operator but I am not sure it would scale well with more entries. Any help would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟凡古楼 2025-01-30 18:35:56

您必须加入多个fans的副本:

SELECT f1.user
FROM fans f1 
INNER JOIN fans f2 ON f2.user = f1.fan 
LEFT JOIN fans f3 ON f3.fan = f2.fan AND f3.user = f1.user 
WHERE f2.fan = ? AND f3.fan IS NULL;

如果您还想要用户的名称:

SELECT u.*
FROM users u
INNER JOIN fans f1 ON f1.user = u.ID
INNER JOIN fans f2 ON f2.user = f1.fan 
LEFT JOIN fans f3 ON f3.fan = f2.fan AND f3.user = f1.user 
WHERE f2.fan = ? AND f3.fan IS NULL;

更改为您想要的用户的ID。

请参阅<
a href =“ https://dbfiddle.uk/?rdbms = mysql_5.7&amp; fiddle = E3532158D379E5BC275345E26A5EB44”

You must join multiple copies of fans:

SELECT f1.user
FROM fans f1 
INNER JOIN fans f2 ON f2.user = f1.fan 
LEFT JOIN fans f3 ON f3.fan = f2.fan AND f3.user = f1.user 
WHERE f2.fan = ? AND f3.fan IS NULL;

If you want the names of the users also:

SELECT u.*
FROM users u
INNER JOIN fans f1 ON f1.user = u.ID
INNER JOIN fans f2 ON f2.user = f1.fan 
LEFT JOIN fans f3 ON f3.fan = f2.fan AND f3.user = f1.user 
WHERE f2.fan = ? AND f3.fan IS NULL;

Change ? to the id of the user that you want.

See the demo.

夏末的微笑 2025-01-30 18:35:56

如果我正确理解,您可以尝试加入用户fans以查找遵循的人,然后两次加入

SELECT *
FROM users u1
WHERE EXISTS (
    SELECT 1
    FROM users uu
    INNER JOIN fans f
    ON uu.id = f.fan 
    WHERE uu.id = 1 AND f.user = u1.id
)

或使用自加入用户两次

SELECT u1.*
FROM users u1
INNER JOIN users uu
INNER JOIN fans f
ON uu.id = f.fan AND f.user = u1.id
WHERE uu.id = 1

sqlfiddle

If I understand correctly, you can try to join users and fans to find who is followed user then do join

SELECT *
FROM users u1
WHERE EXISTS (
    SELECT 1
    FROM users uu
    INNER JOIN fans f
    ON uu.id = f.fan 
    WHERE uu.id = 1 AND f.user = u1.id
)

or using self-join user twice

SELECT u1.*
FROM users u1
INNER JOIN users uu
INNER JOIN fans f
ON uu.id = f.fan AND f.user = u1.id
WHERE uu.id = 1

sqlfiddle

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文