简体   繁体   English

如何从GROUP BY获得COUNT(*)的最大值?

[英]How do I get the max value of COUNT(*) from GROUP BY?

I have this query 我有这个问题

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

which provides these results: 这提供了以下结果:

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

How do I query Pname and Num such that it returns the element names with the highest count? 如何查询PnameNum ,以便返回具有最高计数的元素名称?

The results should look like this: 结果应如下所示:

InkjetPrinters  10
LaserPrinters   10

You can use the HAVING clause specified from a sub query. 您可以使用从子查询指定的HAVING子句。

In MySQL 在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
 )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM