返回所有用户的列表,与他们最“流行”的追随者配对。一个人的追随者越多,他们就越“受欢迎”

Posted

技术标签:

【中文标题】返回所有用户的列表,与他们最“流行”的追随者配对。一个人的追随者越多,他们就越“受欢迎”【英文标题】:Returns a list of all users, paired with their most "popular" follower. The more followers someone has, the more "popular" they are 【发布时间】:2017-04-07 19:32:09 【问题描述】:

为这些人找到最“受欢迎”的追随者。一个人的追随者越多, 它们很“受欢迎”。

我需要 SQL 查询来选择最受欢迎的追随者。

我的表 - (关注者)

id | person_id | follower_person_id
1    1            2
2    1            3
3    2            1
4    2            4
5    3            1
6    3            2
7    3            4
8    4            3

Person_id 1 共有 2 个关注者(person_id 2,person_id 3),person_id 2 共有 2 个关注者(person_id 1,person_id 4),person_id 3 有 共有 3 个关注者(person_id 1、person_id 2、person_id 4)和 person_id 4 共有 1 个关注者(person_id 3)。

因此,person_id 3 是 person_id 1 最受欢迎的追随者, person_id 1 是 person_id 2、person_id 1(或 person_id 2) 是 person_id 3 和 person_id 3 最受欢迎的追随者 person_id 4 最受欢迎。

这里是查询...

SELECT t1.person_id, t1.follower_person_id, t2.cnt
FROM followers AS t1
JOIN (
   SELECT person_id, COUNT(*) AS cnt
   FROM followers
   GROUP BY person_id 
) AS t2  ON t1.follower_person_id = t2.person_id
WHERE t1.person_id = 1
ORDER BY t2.cnt DESC LIMIT 1

上面的查询输出是

person_id, follower_person_id, cnt
-----------------------------------
1,         3,                  3

Here is explanation of above query

此查询仅适用于为特定人查找受欢迎的人,但 我想为所有人找到他们最“受欢迎”的追随者。

所以输出应该是这样的

person_id, follower_person_id, cnt
-----------------------------------
1,         3,                  3
2,         1,                  2
3,         1,                  2
4,         3,                  3

现在我又多了一个person table

id | name 
1    John         
2    Ali         
3    Rohn         
4    Veronica

现在我想将此 id 转换为人名。

最终输出应该像

person_name, follower_person_name, cnt
--------------------------------------
John,        Rohn,                 3
Ali,         John,                 2
Rohn,        John,                 2
Veronica,    Rohn,                 3

我需要 sql 查询来获取这些数据。

【问题讨论】:

你如何定义“他们最“受欢迎”的追随者。” ...请解释清楚.. Person_id 1 共有 2 个关注者(person_id 2,person_id 3),person_id 2 共有 2 个关注者(person_id 1,person_id 4),person_id 3 共有 3 个关注者(person_id 1,person_id 2,person_id 4) 和 person_id 4 共有 1 个关注者 (person_id 3)。因此,person_id 3 是 person_id 1 最受欢迎的粉丝,person_id 1 是 person_id 2 最受欢迎的粉丝,person_id 1(或 person_id 2)是 person_id 3 最受欢迎的粉丝,person_id 3 是 person_id 4 最受欢迎的粉丝。@scaisEdge跨度> 【参考方案1】:

您可以使用以下查询:

SELECT person_name, follower_name, cnt
FROM (
   SELECT person_name, follower_name, cnt,
          @rn := IF(@pname = person_name, @rn + 1,
                    IF(@pname := person_name, 1, 1)) AS rn
   FROM (
      SELECT t3.name AS person_name, t4.name AS follower_name, t2.cnt
      FROM followers AS t1
      JOIN (
         SELECT person_id, COUNT(*) AS cnt
         FROM followers
         GROUP BY person_id 
      ) AS t2  ON t1.follower_person_id = t2.person_id
      JOIN person AS t3 ON t1.person_id = t3.id
      JOIN person AS t4 ON t1.follower_person_id = t4.id   
   ) AS x
   CROSS JOIN (SELECT @rn := 0, @pname := '') AS vars
   ORDER BY person_name, cnt DESC) AS v
wHERE v.rn = 1;  

输出:

person_name follower_name   cnt
--------------------------------
John        Rohn            3
Veronica    Rohn            3
Ali         John            2
Rohn        Ali             2

查询使用变量来获得每组最大的记录。

Demo here

【讨论】:

【参考方案2】:
SELECT p1.name, p2.name, t2.cnt
FROM followers AS t1
JOIN (
   SELECT person_id, COUNT(*) AS cnt
   FROM followers
   GROUP BY person_id 
) AS t2  ON t1.follower_person_id = t2.person_id
join person p1 on t1.person_id=person.id
join person p2 on t1.follower_person_id=person.id
ORDER BY t2.cnt DESC LIMIT 1

【讨论】:

不,这个不工作。有错误。如果我写join person p1 on t1.person_id=p1.id join person p2 on t1.follower_person_id=p2.id。然后它工作。但它没有给出预期的结果【参考方案3】:

也许您可以将它与您选择的语言的一些代码结合起来 尝试以下查询以从高到低对关注者进行排序,并获取第二列的名称:

select f.person_id, p.name
from followers f 
inner join person p on p.id=f.person_id
group by f.person_id order by count(f.follower_person_id) desc

对于每个输出 id(我们称之为FIXED_ID_FROM_PREVIOUS_QUERY,您尝试获取他关注的人并创建第 1 列和第 3 列

select p.name, count(f.follower_person_id)
from followers f 
inner join person p on p.id=f.person_id 
inner join person pf on pf.id=f.follower_person_id where 
f.person_id in (select person_id from followers where follower_person_id=FIXED_ID_FROM_PREVIOUS_QUERY)
group by f.person_id order by count(f.follower_person_id) desc

【讨论】:

以上是关于返回所有用户的列表,与他们最“流行”的追随者配对。一个人的追随者越多,他们就越“受欢迎”的主要内容,如果未能解决你的问题,请参考以下文章

在 Android 上无需绑定的 BLE 配对

在与整数配对的所有数字列表中的元组内创建一个列表[重复]

选择与 Hadoop 用户列表匹配的记录

如何为用户实现关注者,如在 twitter 中

Facebook API:如何在facebook中获取登录用户的关注者列表[重复]

9.9递归和动态规划——打印n对括号的所有有效组合(即左右括号正确配对)