返回所有用户的列表,与最“热门”的关注者配对。有人拥有的粉丝越多,他们就越“受欢迎”

时间:2017-04-07 19:32:10

标签: mysql sql

  

为这些人找到最“热门”的粉丝。有人拥有的粉丝越多,越多   他们是“受欢迎的”。

我需要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

  

此查询仅适用于查找特定人员的热门人物   我希望找到一个与所有人最“流行”的追随者配对。

SO输出应该像

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查询来获取这些数据。

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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

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