我有一张桌子,我每天都会记录用户的多个分数。我正在尝试创建一个查询,在那里我获得每周通过的前5名每周获胜者...
我是否需要在最高分和日期周进行子查询分组?或者我需要做一个2个子查询,一个用于另一个最高得分的日期,然后使用外部查询进行分组?
表格结构如下:
NAME,
SCORE,
DATE
我想出了这个
SELECT *
FROM `highscores`
WHERE id IN ((SELECT id
FROM highscores
WHERE WEEK(date) IN (SELECT DISTINCT WEEK(date)
FROM highscores)
ORDER BY score DESC))
GROUP BY email
ORDER BY date, score DESC
但显然我不能在子查询中使用LIMIT
答案 0 :(得分:1)
我认为这对你有用。它还应该带回来,如果有的话(假设一周之内的第5个最高得分是两个人之间的关系,这将使他们两个回到那个星期,所以你有6行的那个周)。
select *
from highscores x
where x.score >=
(select max(e.score)
from highscores e
where week(e.date) = week(x.date)
and e.score <
(select max(d.score)
from highscores d
where week(d.date) = week(x.date)
and d.score <
(select max(c.score)
from highscores c
where week(c.date) = week(x.date)
and c.score <
(select max(b.score)
from highscores b
where week(b.date) = week(x.date)
and b.score <
(select max(a.score)
from highscores a
where week(a.date) = week(x.date))))))
order by date, score desc