我有一个像这样设置的MySQL表:
+------------+-------------+
| Account Id | Follower Id |
+------------+-------------+
| 1 | 900 |
| 1 | 901 |
| 1 | 902 |
| 2 | 900 |
| 2 | 903 |
| 2 | 904 |
| 3 | 901 |
| 3 | 902 |
+------------+-------------+
我正在尝试计算帐户ID之间关注者的共性。理想的输出是帐户ID(行)与帐户ID(列)的表格,其中包含常见关注者的数量。一个解决方案是做一个
LEFT JOIN of
帐户ID 1 followers with
帐户ID 2 followers.
但是,如果我有多个(> 10)个不同的帐户ID,这会变得很麻烦。有没有办法为任何规模的帐户ID生成此内容?
答案 0 :(得分:1)
检查以下查询关注者是否在表格上方(account_id,follower_id)
SELECT F.account_id as account1,
C.account_id as account2,
count(*) common_follower_count
FROM followers F
LEFT JOIN followers C ON (F.follower_id=C.follower_id and
F.account_id!=C.account_id)
WHERE C.account_id is not null
GROUP BY F.account_id,C.account_id;