0

例如我有以下表结构

 userid  |  score  |      datestamp      |
-----------------------------------------------------
   1     |   44    |  2013-06-01 14:25:55 
   1     |   50    |  2013-06-02 14:25:55 
   2     |   59    |  2011-06-04 09:25:51 
   1     |   60    |  2011-06-10 21:25:15 
   3     |   19    |  2011-06-02 07:25:18
   2     |   70    |  2011-06-05 09:25:51 
   2     |   12    |  2011-06-08 09:25:51 
   3     |   60    |  2011-06-05 07:25:18
   4     |   30    |  2011-06-10 07:25:18
   3     |   20    |  2011-06-08 07:25:18
   4     |   15    |  2011-06-09 07:25:18
   1     |   50    |  2013-06-11 14:25:55 
   2     |   100   |  2013-06-15 14:25:55 
   3     |   160   |  2013-06-12 14:25:55 
   4     |   75    |  2013-06-16 14:25:55 

我需要这样一个查询,我将在其中给出日期限制,它会给我在此期间每个用户的最高分数。例如,如果我给出开始日期 2013-06-01 和结束日期 2011-06-10,它将导致:

 userid  |  score  |      datestamp      |
----------------------------------------------------- 

  1     |   60    |  2011-06-10 21:25:15 

  2     |   70    |  2011-06-05 09:25:51 

  3     |   60    |  2011-06-05 07:25:18

  4     |   30    |  2011-06-10 07:25:18 

此外,如果我也提供用户 ID(使用 IN(1,2,3,4)),那就太好了

4

2 回答 2

2

如果您不需要相应的日期,而只需要最高分数,这将起作用

SELECT
    userid, MAX(score)
FROM
    tablename
WHERE
    datestamp BETWEEN '2011-06-10 00:00:00' AND '2013-06-01 23:59:59'
    -- AND userid IN (1, 2, 3, 4)
GROUP BY
    userid

不过,获取与此相关的日期会更加复杂。

于 2013-07-23T13:09:38.900 回答
1

试试这个查询,

SELECT userid, MAX(score) as score FROM table 
WHERE datestamp BETWEEN '2011-06-10 00:00:00' AND '2013-06-01 23:59:59' AND userid IN(1,2,3,4)
GROUP BY userid
于 2013-07-23T13:13:43.640 回答