#1111 - 无效使用组功能

时间:2017-04-21 15:10:09

标签: mysql

SELECT  departemen_id, departemen_name, COUNT(*)
FROM    departement d
JOIN    employees e
ON      departemen_id = departement_id
GROUP BY departemen_id, departemen_name
HAVING  COUNT(*) = (
            SELECT  MAX(COUNT(*))
            FROM    employees
            GROUP BY departement_id
        );

1 个答案:

答案 0 :(得分:0)

问题是MAX(COUNT(*))对dbms没有任何意义。要遵循该路径,您必须使用另一级别的内部查询。

HAVING  COUNT(*) = (
            SELECT  MAX(CNT) FROM (
                SELECT  departement_id, COUNT(*) as CNT
                FROM    employees
                GROUP BY departement_id
            )
        );

您可以利用MySQL的行数限制功能,您的查询将更简单,性能更佳

SELECT  d.departement_id, d.departement_name, COUNT(*)
FROM    departement d
JOIN    employees e
ON      d.departement_id = e.departement_id
GROUP BY d.departement_id, d.departement_name
ORDER BY COUNT(*) DESC
LIMIT 1;