Mysql 检索INSERT的第n个子查询 摘要
从一张包含各种用户帖子的表格到一个论坛,另一张表格应每天更新前20名海报。帖子存储在Mysql 检索INSERT的第n个子查询 摘要,mysql,subquery,Mysql,Subquery,从一张包含各种用户帖子的表格到一个论坛,另一张表格应每天更新前20名海报。帖子存储在帖子,每日高分保存在他的核心 桌子 实际问题 在上述查询中插入什么而不是?来说明排名 是否有类似于@NTH\u SUBQUERY的变量将在第五次运行SELECT子查询时替换5 更新:表格hiscore应该只容纳前20名海报。我知道桌子的结构可以优化。答案的重点应该是如何确定子查询的当前检索行。对于您的情况,您似乎太喜欢截断 hiscore: the_date (DATE) | user_id(INT) |
帖子
,每日高分保存在他的核心
桌子
实际问题
在上述查询中插入什么而不是?
来说明排名
是否有类似于@NTH\u SUBQUERY
的变量将在第五次运行SELECT
子查询时替换5
更新:表格
hiscore
应该只容纳前20名海报。我知道桌子的结构可以优化。答案的重点应该是如何确定子查询的当前检索行。对于您的情况,您似乎太喜欢截断
hiscore:
the_date (DATE) | user_id(INT) | rank(INT)
并在日期、等级上建立了一个键
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)
);
插入
set @pos=0;
insert into hiscore
select cur_date(), user_id, @pos:=@pos+1
from ...
为了保持表的大小可控,您可能可以每隔几个月删除一次
或者您可以在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)
);
因此,排名是自动递增的(这与每日帖子数量递减的顺序相同)非常感谢您的输入。我尽量避免在问题中提到自动增量
(因为它毫无疑问是有效的)。但是有没有办法确定当前子查询的编号/索引?set@pos:=0;选择cur_date(),user_id,@pos:=@pos+1 from…
谢谢。看起来应该能用的。但是,在phpMyAdmin中运行此查询时,所有行的秩都为零。
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)
);
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
)