我有一个名为'games'的表,其中有一个名为'week'的列。我试图找到一个单独的查询,它将为我提供'周'的最大值以及该表中有多少行具有'周'的最大值的计数。我可以把它分成两个查询:
SELECT MAX(week) FROM games
// store value in a variable $maxWeek
SELECT COUNT(1) FROM games WHERE week = $maxWeek
// store that result in a variable
有没有办法在一个查询中完成所有操作?
答案 0 :(得分:4)
SELECT week, count(*) FROM games GROUP BY week ORDER BY week DESC LIMIT 1;
或
SELECT week, count(*) FROM games WHERE week = (SELECT max(week) FROM games) GROUP BY week;
(可能更快)