检索 INSERT 的第 N 个子查询
摘要
从一个包含用户各种帖子的表到一个论坛,每天都会更新另一个表,其中包含前 20 个发帖者。帖子存储在 posts
中,每日高分存储在 hiscore
中。
表
posts: post_id(PK:INT) | user_id(INT) | ... | timestamp(TIMESTAMP) hiscore: user_id(INT) | rank(INT)
查询
TRUNCATE TABLE `hiscore` ;
INSERT INTO `hiscore` (`user_id`,`rank`)
(
SELECT `user_id`, ???
FROM `posts`
WHERE `timestamp` BETWEEN blah AND blah
GROUP BY `user_id`
ORDER BY COUNT(`post_id`) DESC
LIMIT 20
)
实际问题
在上面的查询中应该插入什么而不是 ???
来计算排名?
是否有像 @NTH_SUBQUERY
这样的变量可以在第五次运行 SELECT
子查询时替换 5?
更新:表hiscore
应该仅包含前20名海报。我知道表结构可以优化。答案的重点应该是如何确定子查询当前检索的行。
Abstract
From a table holding various posts of users to a forum, another table shall be daily updated with the top 20 posters. Posts are stored in posts
, daily high-scores are held in hiscore
.
Tables
posts: post_id(PK:INT) | user_id(INT) | ... | timestamp(TIMESTAMP) hiscore: user_id(INT) | rank(INT)
Query
TRUNCATE TABLE `hiscore` ;
INSERT INTO `hiscore` (`user_id`,`rank`)
(
SELECT `user_id`, ???
FROM `posts`
WHERE `timestamp` BETWEEN blah AND blah
GROUP BY `user_id`
ORDER BY COUNT(`post_id`) DESC
LIMIT 20
)
The actual question
What is to be inserted in the above query instead of ???
to account for the rank?
Is there a variable like @NTH_SUBQUERY
that'll substitute for 5 on the fifth run of the SELECT
subquery?
UPDATE: The table hiscore
is supposed to only hold the top 20 posters. I know the table structure can be optimized. The focus of the answers should be on how to determine the current retrieved row of the sub-query.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您似乎太喜欢
截断
,对于您的情况上构建了一个键
,并在
the_date,rank
插入以保持表大小易于管理,您可能可以几个月删除一次
或者您可以在
rank
上设置 auto_increment因此,排名会自动递增(这与按每日帖子数量降序排列相同)
You seems too fancy on
truncate
, for you casesand built a key on
the_date, rank
insertion
to keep the table size manageable, you probably can delete once in few months
Or you can set an auto_increment on
rank
So, the rank is auto incremented (which is the same as order by number of daily posts descending)