无效使用群组HAVING

时间:2015-04-05 11:51:36

标签: mysql select group-by

我正在尝试编写一个查询来选择选择的最高值。

SELECT especialidad,COUNT(*)
    FROM medicos    
        GROUP BY especialidad
        HAVING COUNT(*)=(SELECT MAX(COUNT(*))
            FROM medicos
                ORDER BY nombre,especialidad);

2 个答案:

答案 0 :(得分:0)

由于order by导致第二个max(count(*))无关,因此您只需执行<{p}}

SELECT especialidad, COUNT(*)
FROM medicos    
GROUP BY especialidad
ORDER BY COUNT(*) DESC
LIMIT 1

答案 1 :(得分:0)

您可以使用order by desc and limit 1

SELECT especialidad,COUNT(*) as total
FROM medicos    
GROUP BY especialidad
order by total desc limit 1