[英]MySQL query to get top 10 point-earners in last week?
I'm trying to write a query that returns the user ID's of the top 10 users who gained the most points in the last 7 days on my web app. 我正在尝试编写一个查询,该查询返回最近7天在我的Web应用程序中获得最多积分的前10位用户的用户ID。
I have three tables that, together, have the info I need. 我有三个表,它们一起提供了我需要的信息。
votingapi_votes
table. votingapi_votes
表。 It has a record for every up/down vote on a comment or node. node
table. node
表。 It can associate a node ID with a user ID so you can figure out who posted the story getting the votes. comments
table, which does the same for comments. comments
表,对评论也是如此。 I believe I need to write a query that selects every vote on a comment or node from the last week from the votingapi_vote table. 我相信我需要编写一个查询,以从votingapi_vote表中选择上一周的评论或节点的所有投票。
Here's the structure of that table: 这是该表的结构:
So I'd SELECT rows of content_type "node" or "comment" with a Unix timestamp greater than time() - 684000. 因此,我会选择Unix时间戳大于time()-684000的content_type“节点”或“注释”行。
Then it needs to 然后它需要
group these votes by "content_id". 将这些投票按“ content_id”分组。
Look up the respective "user_id" values for each content_id in the "node" and "comments" tables so we know who made the nodes and comments. 在“节点”和“评论”表中为每个content_id查找相应的“ user_id”值,以便我们知道谁制作了节点和注释。
Calculate how many points total each user_id gained from his nodes and comments. 计算每个user_id从其节点和注释中获得的总积分。
Sort these user_id's in reverse order and limits it to displaying only the first 10. 以相反的顺序对这些user_id进行排序,并将其限制为仅显示前10个。
Phew. 唷。 That seems like what I need to do.
这似乎是我需要做的。 Now what does that actual query look like?
现在,实际查询是什么样的?
Posting based on OMG Ponies' answer 根据OMG Ponies的回答发布
SELECT x.user_id, SUM(x.total_votes)
FROM (
SELECT n.user_id, SUM(vav.value) AS total_votes
FROM NODE n
JOIN VOTINGAPI_VOTES vav
ON vav.content_id = n.nid
AND vav.content_type = 'node'
WHERE vav.timestamp > NOW() - 684000
GROUP BY n.user_id
UNION
SELECT c.user_id, SUM(vav.value) AS total_votes
FROM COMMENTS c
JOIN VOTINGAPI_VOTES vav
ON vav.content_id = c.cid
AND vav.content_type = 'comment'
WHERE vav.timestamp > NOW() - 684000
GROUP BY c.user_id
) x
GROUP BY
x.user_id
ORDER BY
x.total_votes DESC
LIMIT 10
The problem with the earlier code is that it returns 2 rows per user, 1 for comment, 1 for node. 早期代码的问题在于,每个用户返回2行,注释返回1行,节点返回1行。 This code will do another
SUM
to aggregate it to just 1 number per user. 此代码将执行另一个
SUM
以将其聚合为每个用户仅1个数字。
Use: 采用:
SELECT x.*
FROM (SELECT n.user_id,
SUM(vav.value) 'total_votes'
FROM NODE n
JOIN VOTINGAPI_VOTES vav ON vav.content_id = n.nid
AND vav.content_type = 'node'
WHERE vav.timestamp > NOW() - 684000
GROUP BY n.user_id
UNION
SELECT c.user_id,
SUM(vav.value) 'total_votes'
FROM COMMENTS c
JOIN VOTINGAPI_VOTES vav ON vav.content_id = c.cid
AND vav.content_type = 'comment'
WHERE vav.timestamp > NOW() - 684000
GROUP BY c.user_id) x
ORDER BY x.total_votes DESC
LIMIT 10
Can you just use the Views module ? 您可以只使用“ 视图”模块吗? I'm pretty sure you can...
我很确定你可以...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.