简体   繁体   English

MySQL表联接

[英]MySQL Table Joins

I have a table which links the userID and the friendID (which is another user) to say they are friends. 我有一个表,该表链接了userID和friendID(这是另一个用户),说他们是朋友。 If User 1 adds User 24 as their friend, the first row in the below table will happen If User 10 adds User 1 as their friend, the second row in the below table will happen 如果用户1将用户24添加为他们的朋友,则下表中的第一行将发生。如果用户10将用户1添加为他们的朋友,则下表中的第二行将发生。

From there, even if User 1 is on either side, they are friends with the adjacent ID. 从那里,即使用户1在任一侧,他们也是具有相邻ID的朋友。

FRIENDS TABLE:

userID | friendID
-----------------
   1   |    24
   10  |    1

What I need to do now is: If User 1 is logged in, I need to display the people they are friends with. 我现在需要做的是:如果用户1已登录,则需要显示他们的朋友。 This query I have wrote: 我写的这个查询:

SELECT DISTINCT (Users.username), friends.userID, friends.friendID FROM Users, friends WHERE Users.userID IN(SELECT userID FROM friends WHERE Users.userID = friends.userID) OR Users.userID IN(SELECT DISTINCT(userID) FROM friends WHERE Users.userID = friends.friendID)

Will bring back: 将带回来:

username | userID | friendID
-----------------------------
   name1 |   10   |    1
   name1 |   1    |    24
   name2 |   10   |    1
   name2 |   1    |    24

name1 = ID 1 name2 = ID 10 名称1 = ID 1名称2 = ID 10

Is there a way where I can retrieve the logged in Users'friends (eg if ID #1 is logged in) whether they appear in the userID or friendID columns. 有没有一种方法可以检索已登录的用户朋友(例如,如果已登录ID#1),而不管它们出现在userID还是friendID列中。 and then, in this case, retrieve ID 24 and 10 because they are linked with ID #1, and then when it comes to displaying, eliminate ID # 1 from the list because it will bring up that they are friends with themselves. 然后,在这种情况下,请检索ID 24和10,因为它们与ID#1链接在一起,然后在显示时,从列表中删除ID#1,因为它会显示出他们是自己的朋友。

Hope this makes sense and thank you in advance! 希望这有道理,并提前感谢您!

SELECT DISTINCT s.FriendId
FROM (
    SELECT f.FriendId
    FROM   Friends f
    WHERE  f.UserId = @id
    UNION ALL
    SELECT f.UserId
    FROM   Friends f
    WHERE  f.FriendId = @id
) s
WHERE s.FriendId != @id

You may not need the last WHERE given that a user probably is not adding himself or herself as a friend. 考虑到用户可能没有将自己添加为朋友,您可能不需要最后一个WHERE。

While the way you are storing data may seem efficient, the following may be a better approach: When a MUTUAL friendship is formed, enter the data twice: once as userID(1) friendID(24) and once as userID(24) friendID(1). 虽然您似乎可以高效地存储数据,但以下方法可能是更好的方法:形成相互友谊时,请两次输入数据:一次作为userID(1)friendID(24),一次作为userID(24)friendID( 1)。 The reason this approach may be good is that it makes your table reusable. 这种方法之所以好,是因为它使您的表可重用。

Presently your table is like facebook: if I am your friend then perforce you are also my friend: I see your activities; 目前您的餐桌就像Facebook:如果我是您的朋友,那么强迫您也是我的朋友:我看到您的活动; you see my activities. 你看我的活动。 The design I explain allow you to use the table as both facebook and twitter: just because you are following my activities does not mean I want to follow yours. 我解释的设计允许您将表同时用作Facebook和Twitter:仅仅因为您关注我的活动并不意味着我想关注您的活动。

Here's an easy way to do it with UNION . 这是使用UNION的简单方法。 This also takes care of duplicates: 这也可以处理重复项:

(SELECT userID
FROM friends
WHERE friendID = 1)
UNION
(SELECT friendID
FROM friends
WHERE userID = 1)

If you want to return usernames too, you can do your JOINs inside the subqueries: 如果您也想返回用户名,则可以在子查询中执行JOIN:

(SELECT f.userID, u.username
FROM friends f
JOIN users u
  ON u.userID = f.userID
WHERE f.friendID = 1)
UNION
(SELECT f.friendID, u.username
FROM friends f
JOIN users u
  ON u.userID = f.friendID
WHERE f.userID = 1)

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

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