gpt4 book ai didi

mysql - 如何从 GROUP BY 中获取 COUNT(*) 的最大值?

转载 作者:行者123 更新时间:2023-11-30 22:20:25 25 4
gpt4 key购买 nike

我有这个问题

SELECT Pname, COUNT(*) AS Num
FROM employee
JOIN project
ON Dno = Dnum
GROUP BY Pname

它提供了这些结果:

Pname                   Num
Computerization 3
DatabaseSystems 8
InkjetPrinters 10
LaserPrinters 10
Middleware 8
Newbenefits 3
OperatingSystems 8
ProductX 4
ProductY 4
ProductZ 4
Reorganization 1

如何查询 PnameNum 以返回计数最高的元素名称?

结果应该是这样的:

InkjetPrinters  10
LaserPrinters 10

最佳答案

您可以使用从子查询中指定的 HAVING 子句。

在 MySQL 中

 SELECT Pname, COUNT(*) AS Num
FROM employee
JOIN project
ON Dno = Dnum
GROUP BY Pname
HAVING COUNT(*) = (
SELECT COUNT(*)
FROM employee
JOIN project
ON Dno = Dnum
GROUP BY Pname
ORDER BY COUNT(*) DESC
LIMIT 1
)

关于mysql - 如何从 GROUP BY 中获取 COUNT(*) 的最大值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36757855/

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