检索 INSERT 的第 N 个子查询

发布于 2024-10-14 13:15:57 字数 792 浏览 2 评论 0原文

摘要

从一个包含用户各种帖子的表到一个论坛,每天都会更新另一个表,其中包含前 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

数理化全能战士 2024-10-21 13:15:57
INSERT INTO `hiscore` (`user_id`,`rank`)
 (
   SELECT `user_id`, @rank = @rank + 1
   FROM `posts`, (SELECT @rank := 0) r
   WHERE `timestamp` BETWEEN blah AND blah
   GROUP BY `user_id`
   ORDER BY COUNT(`post_id`) DESC
   LIMIT 20
 )
INSERT INTO `hiscore` (`user_id`,`rank`)
 (
   SELECT `user_id`, @rank = @rank + 1
   FROM `posts`, (SELECT @rank := 0) r
   WHERE `timestamp` BETWEEN blah AND blah
   GROUP BY `user_id`
   ORDER BY COUNT(`post_id`) DESC
   LIMIT 20
 )
咋地 2024-10-21 13:15:57

您似乎太喜欢截断,对于您的情况

hiscore:
   the_date (DATE) | user_id(INT) | rank(INT)

上构建了一个键

,并在the_date,rank 插入

set @pos=0;

insert into hiscore
select cur_date(), user_id, @pos:=@pos+1
from ...

以保持表大小易于管理,您可能可以几个月删除一次

或者您可以在 rank 上设置 auto_increment

create table hiscore
(
  the_date date not null,
  rank int(3) not null auto_increment,
  user_id int(10) not null,
  primary key (the_date, rank)
);

因此,排名会自动递增(这与按每日帖子数量降序排列相同)

You seems too fancy on truncate, for you cases

hiscore:
   the_date (DATE) | user_id(INT) | rank(INT)

and built a key on the_date, rank

insertion

set @pos=0;

insert into hiscore
select cur_date(), user_id, @pos:=@pos+1
from ...

to keep the table size manageable, you probably can delete once in few months

Or you can set an auto_increment on rank

create table hiscore
(
  the_date date not null,
  rank int(3) not null auto_increment,
  user_id int(10) not null,
  primary key (the_date, rank)
);

So, the rank is auto incremented (which is the same as order by number of daily posts descending)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文