MYSQL查询获取值在两个日期之间增加最多的记录

时间:2011-06-01 10:53:00

标签: mysql

我有一个MySQL表ScoreArchive,其中包含以下字段:

ID(int),主键

Date(日期)

Score(int)

我每天在表格中记录每个Score的{​​{1}}。

现在,我希望找到最高分数增加的ID,例如,2011-04-22和2011-05-31。

如何使用MySQL查询找到这些?

2 个答案:

答案 0 :(得分:3)

尝试类似:

select id, max(score) - min(score) as diff ... group by id order by diff desc

编辑(跟进评论):

或类似的东西:

select id, final_scores.score - start_scores.score as diff
from (
   select id, min(date) as min_date, max(date) as max_date
   from scores
   where date between ...
   group by id
   ) as ranges
join scores as final_scores
on final_scores.date = ranges.min_date
join scores as start_scores
on start_scores.date = ranges.max_date
where ...
order by diff desc

答案 1 :(得分:0)

SELECT score FROM ScoreArchive WHERE date BETWEEN 2011-04-22 AND 2011-05-31 ORDER BY score DESC;

这就是我如何在pgsql中做到这一点我猜测mysql是相同的