如何确定哪个用户是特定标签中的顶级用户?

How can I determine which user is the top one in the specific tag?

我有一个类似 Whosebug 的问答网站。以下是一些表的结构:

-- {superfluous} means some other columns which are not related to this question

// q&a
+----+-----------------+--------------------------+------+-----------+-----------+
| id |      title      |            body          | type |  related  | author_id |
+----+-----------------+--------------------------+------+-----------+-----------+
| 1  | How can I ...   | I'm trying to make ...   |  q   | NULL      | 3         |
| 2  |                 | You can do that by ...   |  a   | 1         | 1         |
| 3  | Why should I .. | I'm wonder, why ...      |  q   | NULL      | 1         |
| 4  |                 | First of all you ...     |  a   | 1         | 2         |
| 5  |                 | Because that thing ...   |  a   | 3         | 2         |
+----+-----------------+--------------------------+------+-----------+-----------+

// users
+----+--------+-----------------+
| id |  name  |  {superfluous}  |
+----+--------+-----------------+
| 1  | Jack   |                 |
| 2  | Peter  |                 |
| 3  | John   |                 |
+----+--------+-----------------+

// votes
+----+----------+-----------+-------+-----------------+
| id |  user_id |  post_id  | value |  {superfluous}  |
+----+----------+-----------+-------+-----------------+
| 1  | 3        |  4        |  1    |                 |
| 2  | 1        |  1        | -1    |                 |
| 3  | 2        |  1        |  1    |                 |
| 4  | 3        |  2        | -1    |                 |
| 5  | 1        |  4        |  1    |                 |
| 6  | 3        |  5        | -1    |                 |
+----+--------+-------------+-------+-----------------+

// tags
+----+------------+-----------------+
| id |    name    |  {superfluous}  |
+----+------------+-----------------+
| 1  | PHP        |                 |
| 2  | SQL        |                 |
| 3  | MySQL      |                 |
| 4  | HTML       |                 |
| 5  | CSS        |                 |
| 6  | C#         |                 |
+----+------------+-----------------+

// q&aTag
+-------+--------+
| q&aid | tag_id |
+-------+--------+
| 1     | 1      |
| 1     | 4      |
| 3     | 5      |
| 3     | 4      |
| 4     | 6      |
+-------+--------+

现在我需要找到特定标签中的热门用户。例如,我需要在 PHP 标签中找到 Peter 作为顶级用户。因为他对问题 1 (具有 PHP 标签) 的回答获得了 2 个赞成票。这样做可能吗?

试试这个:

select q1.title, u.id, u.name, sum(v.value) total from `q&a` q1
left join `q&atag` qt ON q1.id = qt.`q&aid`
inner join tags t ON qt.tag_id = t.id
left join `q&a` q2 ON q2.related = q1.id
left join users u ON q2.author_id = u.id
left join votes v ON v.post_id = q2.id

where t.name = 'PHP'
group by q1.id, u.id

这是一个简单的分割解决方案:

让我们把它分成子查询:

  1. 获取您要搜索的标签的 idselect id from tags where name = 'PHP'
  2. 获取带有此标签的问题:select 'q&aid' from 'q&aTag' where tag_id = 1.
  3. 得到 id 个问题的答案:select id, author_id fromq&awhere related in (2.)
  4. 获取最终查询:select user_id, sum(value) from votes where post_id in (3.) group by user_id

现在将它们全部组合起来得到结果:

select user_id, sum(`value`) total from votes
where post_id in (
    select id from `q&a` where related in (
        select `q&aid` from `q&aTag` where tag_id IN (
            select id from tags where name = 'PHP'
        )
    )
)
group by user_id

如果只需要一条记录,可以在末尾添加:

order by total desc limit 1