старый вопрос, но я нашел великолепный ответ (не мой), используя переменные сеанса.
Учитывая таблицу
CREATE TABLE `report` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`reportDate` int(11) DEFAULT NULL,
`count` int(11) DEFAULT NULL,
`parameter` varchar(100) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `INDEX1` (`reportDate`,`parameter`,`count`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;
Следующий запрос выберет 10 лучших параметров в день на основе их количества в этот день
SELECT parameter, reportDate, count
FROM
(SELECT parameter,
reportDate,
count,
@reportDate_rank := IF(@current_reportDate = reportDate,@reportDate_rank + 1, 1) AS reportDate_rank,
@current_reportDate := reportDate
FROM report
ORDER BY reportDate, count DESC
) dateRanked
WHERE reportDate_rank <= 10;