将来自 Union 的 SQL 结果分组

发布于 2024-11-07 19:46:30 字数 1085 浏览 5 评论 0原文

我的数据库中有一个表,用于用户互相关注。

该表如下:

UniqueID
FollowerID
FollowedUserID

FollowID 是关注其他人的用户 ID。

FollowedUserID 是被 FollowID 用户关注的人的 UserID。

我想从该表中基于单个用户检索连接列表。我的查询应返回当前用户关注或正在关注的所有其他 UserID,且没有重叠。

假设我们这里有几个条目:

FollowerID   FollowedUserID
    1              2
    1              3
    4              1
    2              1

这将表明用户 1 正在关注用户 2 和 3,因此用户 2 和 3 正在被用户 1 关注。另一方面,它显示用户 1 后面跟着用户 2 和 3。

我想做的是找出用户 1 拥有的联系,因此查询应返回用户:2、3 和 4(用户 1 正在关注用户 2 和 3,并且用户 4 正在关注)

如何通过单个查询实现此目的?

连接被视为被关注或关注其他人,因此可能会出现一些重复的结果(如果两个用户都互相关注)。我希望能够对这些结果进行分组,以便结果是不同的。

我尝试过类似于以下内容的 UNION 查询:

SELECT FollowerID, FollowedUserID From Follows WHERE FollowerID = 1
UNION
SELECT FollowerID, FollowedUserID FROM Follows WHERE FollowedUserID = 1

理论上,我想将 FollowerID 和 FollowedUserID 组合在一起以保持它们不同。

我对检索非不同的结果然后在 php 端创建唯一结果的数据集不感兴趣——查询应该只返回不同的值。

I have a table in my database used for users to follow eachother.

The table goes as follows:

UniqueID
FollowerID
FollowedUserID

FollowID is the UserID of the person who is following someone else.

FollowedUserID is the UserID of the person who is being followed by the FollowID User.

I'd like to retrieve a list of connections from this table based off a single user. My query should return all the other UserIDs that the current user is either following or being followed by with no overlaps.

So say we have a few entries here:

FollowerID   FollowedUserID
    1              2
    1              3
    4              1
    2              1

This would show that the User 1 is following users 2 and 3, and thus users 2 and 3 are being followed by user 1. On the other side of the spectrum it shows that User 1 is followed by users 2 and 3.

What I'd like to do is find out the connections that user 1 has, so the query should return users: 2, 3, and 4 (user 1 is following users 2 and 3 and is being followed by user 4)

How can I achieve this from a single query?

A connection is considered either being followed or following someone else, so it is possible that some duplicate results can occur (if both users are following eachother). I'd like to be able to group these results so that the result is distinct.

I've tried a UNION query similar to:

SELECT FollowerID, FollowedUserID From Follows WHERE FollowerID = 1
UNION
SELECT FollowerID, FollowedUserID FROM Follows WHERE FollowedUserID = 1

Theoretically I'd like to Group FollowerID and FollowedUserID together to keep them distinct.

I'm not interested in retrieving a non-distinct result and then creating a dataset of unique results php side -- the query should return distinct values only.

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

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

发布评论

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

评论(2

仅此而已 2024-11-14 19:46:30

您很接近,但您必须反转 UNION 分支之一中的列顺序:

SELECT FollowerID AS reference_user, FollowedUserID AS connection
  FROM Follows
 WHERE FollowerID = 1
UNION
SELECT FollowedUserID AS reference_user, FollowerID AS connection
  FROM Follows
 WHERE FollowedUserID = 1
GROUP BY reference_user;

请注意,如果删除 WHERE 子句,您将获得每个人的所有连接。

You were close, but you have to reverse the order of the columns in one of the branches of the UNION:

SELECT FollowerID AS reference_user, FollowedUserID AS connection
  FROM Follows
 WHERE FollowerID = 1
UNION
SELECT FollowedUserID AS reference_user, FollowerID AS connection
  FROM Follows
 WHERE FollowedUserID = 1
GROUP BY reference_user;

Note that if you drop the WHERE clauses, you get all connections for everyone.

山有枢 2024-11-14 19:46:30

我认为您需要修改您的查询:

edit: removed the un-needed select distinct
SELECT FollowedUserID as ID From Follows WHERE FollowerID = 1
UNION
SELECT FollowerID as ID FROM Follows WHERE FollowedUserID = 1

但这并不能完全回答方向问题。

SELECT FollowedUserID as ID, "follow" as direction 
From Follows 
WHERE FollowerID = 1
UNION
SELECT FollowerID as ID, "followed" as direction 
FROM Follows 
WHERE FollowedUserID = 1

现在您也知道连接的方向了。

I think you'd need to revise your query:

edit: removed the un-needed select distinct
SELECT FollowedUserID as ID From Follows WHERE FollowerID = 1
UNION
SELECT FollowerID as ID FROM Follows WHERE FollowedUserID = 1

But that doesn't quite answer the question of direction.

SELECT FollowedUserID as ID, "follow" as direction 
From Follows 
WHERE FollowerID = 1
UNION
SELECT FollowerID as ID, "followed" as direction 
FROM Follows 
WHERE FollowedUserID = 1

Now you know the direction of the connection as well.

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