我目前想要使用此查询从我的MySQL数据库中选择目前排名前25位的团队:
SELECT team_school, team_name, rank, rank_name, week_key, year_key
FROM teams t JOIN weeklyrankings wr ON t.team_key = wr.team_key
JOIN RankType rt ON wr.rank_type_key = rt.rank_key
WHERE wr.rank <= 25
AND wr.week_key = (
SELECT MAX(week_key)
FROM weeklyrankings
)
AND wr.year_key = (
SELECT MAX(year_key)
FROM weeklyrankings
)
GROUP BY t.team_key
ORDER BY wr.rank;
它有效,但我猜测WHERE子句中的两个子查询不是最佳的。我尝试用以下代码替换两个子查询:
AND wr.week_key = MAX(wr.week_key)
AND wr.year_key = MAX(wr.year_key)
但是收到错误[HY000][1111] Invalid use of group function
那么什么是更好的方法来确保我获得年度和&amp;&amp;的最大记录周?