MySQL关注者使用联接查询

时间:2015-11-06 17:29:22

标签: mysql sql join

我有两张桌子:粉丝:

  followee_id | followers_id
--------------|--------------
       1      |      2
       1      |      3
       2      |      3
       3      |      1

和用户:

| user_id | user_name | etc... 
|---------|-----------|-------
|    1    |   user1   |
|    2    |   user2   |
|    3    |   user3   |

以及一个查询,根据其关注者数量选择排名前50位的用户:

SELECT followers.followee_id AS user_id, COUNT(*) followers, users.user_name, users.user_profile_picture, users.display_name
FROM followers
INNER JOIN users
ON users.user_id = followers.followee_id
GROUP BY followee_id ORDER BY followers DESC LIMIT 50 

以及一个选择您是否遵循它们的查询:

SELECT followers.followee_id, 1 AS follows_them FROM followers WHERE followers_id = 1

我需要加入他们,以便我有一个表(用户3)显示:

| user_id | user_name | followers | follows_them | follow_you | etc...
----------|-----------|-----------|--------------|------------|-----------
     1    |   user1   |      2    |       1      |      1     |
     2    |   user2   |      1    |       1      |      2     |
     3    |   user3   |      1    |       0      |      0     |

表格按用户拥有的粉丝数量排序

SQLFiddle: http://sqlfiddle.com/#!9/17815

3 个答案:

答案 0 :(得分:1)

取决于数据:

(1,2),
(1,3),
(2,3),
(3,1);

结果是:

User_id = 1 has 1 follower
User_id = 2 has 1 follower
User_id = 3 has 1 follower

User_id = 1 follows 1 user (user_id = 3)
User_id = 2 follows 1 user (user_id = 1)
User_id = 3 follows 2 users (user_id = 1, user_id = 2)

SELECT bb.user_id, bb.user_name,
    (
        SELECT COUNT(*) AS a
        FROM followers
        WHERE followee_id = bb.user_id
        GROUP BY followee_id
    ) AS followers,
    (
        SELECT COUNT(*) AS b
        FROM followers
        WHERE followers_id = bb.user_id
        GROUP BY followers_id
    ) AS follows_them
FROM followers AS aa
INNER JOIN users AS bb
ON aa.followee_id = bb.user_id
GROUP BY bb.user_id
ORDER BY followers DESC
LIMIT 50;

user_id user_name   followers   follows_them
1       User1       2           1
3       User3       1           2
2       User2       1           1

更新:

SELECT bb.user_id, bb.user_name,
    (
        SELECT COUNT(*)
        FROM followers
        WHERE followee_id = bb.user_id
        GROUP BY followee_id
    ) AS followers,
    (
        SELECT (CASE WHEN COUNT(*) > 0 THEN 1 ELSE 0 END)
        FROM followers
        WHERE followers_id = bb.user_id
        GROUP BY followers_id
    ) AS follows_them,
    (
        SELECT (CASE WHEN COUNT(*) > 0 THEN 1 ELSE 0 END)
        FROM followers
        WHERE followee_id = bb.user_id
        GROUP BY followee_id
    ) AS follow_you
FROM followers AS aa
INNER JOIN users AS bb
ON aa.followee_id = bb.user_id
GROUP BY bb.user_id
ORDER BY followers DESC
LIMIT 50;

结果:

user_id user_name   followers   follows_them    follow_you
1       User1       2           1               1
3       User3       1           1               1
2       User2       1           1               1

答案 1 :(得分:1)

<强> SqlFiddleDemo

请仔细检查,我的主要lenguaje不是英语,所以不确定谁跟谁。

SELECT 
      T.*,
      if(f_to.followee_id is null, 'no', 'yes') is_followby_userid,
      if(f_him.followee_id is null, 'no', 'yes') is_following_userid      
FROM 
    (
      SELECT followers.followee_id AS user_id,
          COUNT(*) followers,
          users.user_name,
          users.user_profile_picture,
          users.display_name
      FROM followers
      INNER JOIN users
          ON users.user_id = followers.followee_id
      GROUP BY followee_id
      ORDER BY followers DESC LIMIT 50
    ) T
LEFT JOIN followers f_to
       ON T.user_id = f_to.followee_id
      and f_to.followers_id = 1    -- your @user_id
LEFT JOIN followers f_him       
       ON T.user_id = f_him.followers_id
      and f_him.followee_id = 1    -- your @user_id

<强>输出

| user_id | followers | user_name | user_profile_picture | display_name | is_followby_userid | is_following_userid |
|---------|-----------|-----------|----------------------|--------------|--------------------|---------------------|
|       1 |         2 |     User1 |       1_562a7cb9.jpg |     User One |                 no |                  no |
|       2 |         1 |     User2 |       2_562b7cb9.jpg |     User Two |                 no |                 yes |
|       3 |         1 |     User3 |       3_562c7cb9.jpg |   User Three |                yes |                 yes |

答案 2 :(得分:0)

另一种选择:

SELECT flws.followee_id AS user_id,
        users.user_name,
        COUNT(flws.followers_id) followers,
        CASE WHEN MAX(flwsthem.followee_id) IS NULL
                THEN 0
                ELSE 1
            END AS follows_them,
        CASE WHEN MAX(flwsyou.followee_id) IS NULL
                THEN 0
                ELSE 1
            END AS follows_you

FROM followers flws
  INNER JOIN users ON users.user_id = flws.followee_id
  LEFT OUTER JOIN followers flwsthem ON flws.followee_id = flwsthem.followee_id AND flwsthem.followers_id = 1
  LEFT OUTER JOIN followers flwsyou ON flws.followee_id = flwsyou.followers_id AND flwsyou.followee_id = 1

GROUP BY flws.followee_id
ORDER BY followers DESC
LIMIT 50

<强>输出

| followee_id | user_name | followers | follows_them | follows_you |
|           1 |     User1 |         2 |            0 |           0 |
|           3 |     User3 |         1 |            1 |           1 |
|           2 |     User2 |         1 |            0 |           1 |

http://sqlfiddle.com/#!9/17815/17