mysql有和聚合函数

时间:2017-05-26 12:29:24

标签: mysql

我试着用max来获得所有“balcao”。 “nCli”的值

SELECT cli.idBalcao, count(*) AS nCLientes
  FROM cliente AS cli
  GROUP BY cli.idBalcao
  HAVING nClientes=MAX(nClientes)

任何人都知道为什么不起作用? 我使用用户定义的变量解决了它,但是这段代码应该适用于MySQL。

1 个答案:

答案 0 :(得分:0)

如果将其更改为

,该怎么办?
SELECT cli.idBalcao, count(*) AS nCLientes,
MAX(nCli) as maxClientes
  FROM cliente AS cli
  GROUP BY cli.idBalcao
  HAVING nClientes = maxClientes

(OR)

SELECT cli.idBalcao, count(*) AS nCLientes
  FROM cliente AS cli
  GROUP BY cli.idBalcao
  HAVING nClientes IN (select MAX(nCli) from cliente)