mysql - Returns a list of all users, paired with their most "popular" follower. The more followers someone has, the more "popular" they are -
find "popular" follower persons. more followers has, more "popular" are.
i need sql query select popular follower of people.
my table - (followers)
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 has total 2 follower (person_id 2, person_id 3), person_id 2 has total 2 followers (person_id 1, person_id 4), person_id 3 has total 3 followers (person_id 1, person_id 2, person_id 4)and person_id 4 has total 1 followers (person_id 3).
therefore, person_id 3 popular follower person_id 1, person_id 1 popular follower person_id 2, person_id 1 (or person_id 2) popular follower person_id 3 , person_id 3 popular person_id 4.
here query...
select t1.person_id, t1.follower_person_id, t2.cnt followers t1 join ( select person_id, count(*) cnt followers group person_id ) t2 on t1.follower_person_id = t2.person_id t1.person_id = 1 order t2.cnt desc limit 1 above query output is
person_id, follower_person_id, cnt ----------------------------------- 1, 3, 3 here explanation of above query
this query work find popular person specific person want find paired "popular" follower person.
so output should like
person_id, follower_person_id, cnt ----------------------------------- 1, 3, 3 2, 1, 2 3, 1, 2 4, 3, 3 now have person table
id | name 1 john 2 ali 3 rohn 4 veronica now want convert id person name.
final output shoud like
person_name, follower_person_name, cnt -------------------------------------- john, rohn, 3 ali, john, 2 rohn, john, 2 veronica, rohn, 3 i need sql query data.
you can use following query:
select person_name, follower_name, cnt ( select person_name, follower_name, cnt, @rn := if(@pname = person_name, @rn + 1, if(@pname := person_name, 1, 1)) rn ( select t3.name person_name, t4.name follower_name, t2.cnt followers t1 join ( select person_id, count(*) cnt followers group person_id ) t2 on t1.follower_person_id = t2.person_id join person t3 on t1.person_id = t3.id join person t4 on t1.follower_person_id = t4.id ) x cross join (select @rn := 0, @pname := '') vars order person_name, cnt desc) v v.rn = 1; output:
person_name follower_name cnt -------------------------------- john rohn 3 veronica rohn 3 ali john 2 rohn ali 2 the query uses variables in order greatest-per-group record.
Comments
Post a Comment