gpt4 book ai didi

mysql - #1111 - 组功能使用无效

转载 作者:行者123 更新时间:2023-11-29 19:05:32 25 4
gpt4 key购买 nike

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
);

最佳答案

问题是 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;

关于mysql - #1111 - 组功能使用无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43546192/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com