我正在使用 Mysql 并有这些表:(只显示重要的列)
人物
id, 主键
发布
id,主键
点, INT
参观
id,主键
person_id,指Person
post_id,指的是帖子
我想找到的是总体得分最高的人(前 5 名)?以及每个帖子上得分最高的人。
有人可以指导我吗?非常感谢任何帮助!
最佳答案
总分最高的前 5 名:
SELECT
p.id,
SUM(Post.points) AS total_points
FROM
Person p
INNER JOIN Visit v
ON p.id = v.person_id
INNER JOIN Post
ON v.post_id = Post.id
GROUP BY
p.id
ORDER BY
SUM(Post.points) DESC
LIMIT 5
一篇文章中得分最多的前 5 个人:
SELECT
p.id,
MAX(Post.points) AS best_post_points
FROM
Person p
INNER JOIN Visit v
ON p.id = v.person_id
INNER JOIN Post
ON v.post_id = Post.id
GROUP BY
p.id
ORDER BY
MAX(Post.points) DESC
LIMIT 5
前 5 个帖子:
SELECT
p.id,
Post.points
FROM
Person p
INNER JOIN Visit v
ON p.id = v.person_id
INNER JOIN Post
ON v.post_id = Post.id
ORDER BY
Post.points DESC
LIMIT 5
关于mysql - 查询寻找得分最多的人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9117518/