我想要一个特定月份+一年中所有玩家的最后一排


I want the last row of all players in a specific month+year

这是我的2个月sql表的简化版本(ORDERED BY DATE):

player_id|日期 nbsp nbsp nbsp nbsp nbsp 得分
1 nbsp nbsp nbsp nbsp nbsp nbsp nbsp;2011年5月25日 nbsp;1200
2 nbsp nbsp nbsp nbsp nbsp nbsp nbsp;2011年5月25日 nbsp;3400
3 nbsp nbsp nbsp nbsp nbsp nbsp nbsp;2011年5月26日 nbsp;3200
4 nbsp nbsp nbsp nbsp nbsp nbsp nbsp;2011年5月26日 nbsp;4400
1 nbsp nbsp nbsp nbsp nbsp nbsp nbsp;2011年5月28日 nbsp;1000
2 nbsp nbsp nbsp nbsp nbsp nbsp nbsp;2011年5月28日 nbsp;2000
3 nbsp nbsp nbsp nbsp nbsp nbsp nbsp;2011年5月29日 nbsp;3000
4 nbsp nbsp nbsp nbsp nbsp nbsp nbsp;2011年5月29日 nbsp;4000

1 nbsp nbsp nbsp nbsp nbsp nbsp nbsp;2011年6月24日 nbsp;1300
2 nbsp nbsp nbsp nbsp nbsp nbsp nbsp;2011年6月24日 nbsp;2500
3 nbsp nbsp nbsp nbsp nbsp nbsp nbsp;2011年6月24日 nbsp;5000
4 nbsp nbsp nbsp nbsp nbsp nbsp nbsp;2011年6月24日 nbsp;3000

基本上,我想要一个显示所有玩家在特定月份/特定年份的最后得分的查询。

示例:

如果我想要05月份所有玩家的最终分数,请查看结果将是:
1 nbsp 2011年5月28日 nbsp 1000
2 nbsp 2011年5月28日 nbsp 2000
3 nbsp 2011年5月29日 nbsp 3000
4 nbsp 2011年5月29日 nbsp 4000

到目前为止我的sql查询:

  SELECT m1.* FROM table m1 
    LEFT JOIN table m2 ON (m1.player_id = m2.player_id AND m1.date < m2.date) 
    WHERE m2.date IS NULL 
    AND month(m1.date) = 05 
    AND year(m1.date) = 2011 
    ORDER BY score DESC); 

这似乎并没有显示所有的球员,只有05后几个月没有上场的球员。我在哪里添加日期选择?

**编辑约翰·内斯特里亚克的回答对我很有帮助:)

我认为他指的是这里显示的技术:检索每组中的最后一条记录

另外一个限制是,他不想要最后一张唱片,而是某个月的最后一张。

奇怪的是,您必须两次提供额外的约束,一次是在联接条件中,另一次是过滤结果。这应该为你做。

SELECT m1.* FROM table m1 
  LEFT JOIN table m2 ON 
    (m1.player_id = m2.player_id AND m1.date < m2.date 
     AND m2.date < '2011-06-01') 
  WHERE m2.date IS NULL AND month(m1.date) = 5 AND year(m1.date) = 2011

假设Unique:中的(player_id, date)组合

SELECT 
      t.*
FROM 
      TableX AS t
  JOIN
      ( SELECT 
              player_id
            , MAX(date) AS maxDate
        FROM
            TableX
        WHERE 
             date BETWEEN '2011-05-01'
                      AND LAST_DAY('2011-05-01')
        GROUP BY
              player_id
      ) AS tg
    ON  
      (tg.player_id, tg.maxDate) = (t.player_id, t.date)
ORDER BY 
      t.score DESC