简体   繁体   English

SELECT COUNT() 在 SELECT MAX() SQL 中

[英]SELECT COUNT() INSIDE A SELECT MAX() SQL

I've got this table called player_mast in a db (data are just an example), and I want to find the club which supplied the most number of players to the 2016 EURO cup.我在数据库中有一个名为player_mast表(数据只是一个例子),我想找到为 2016 年欧洲杯提供最多球员的俱乐部。

player_id玩家 ID country_id country_id jersey_no球衣号码 player_name参赛者姓名 posi_to_play posi_to_play dt_of_bir dt_of_bir age年龄 playing_club play_club
1231 1231 1231 1231 10 10 Hazard冒险 striker前锋 2/3/1991 2/3/1991 33 33 Chelsea切尔西

Why this query doesn't work?为什么这个查询不起作用? It seems right to me:在我看来是正确的:

SELECT playing_club, MAX(NumberOfPlayerForTeam)
FROM (
  SELECT playing_club, COUNT(player_id) AS NumberOfPlayerForTeam
  FROM player_mast
  GROUP BY(playing_club))
GROUP BY(playing_club);

Try this试试这个

SELECT playing_club, NumberOfPlayerForTeam<br>
FROM (<br>
  SELECT playing_club, COUNT(player_id) AS NumberOfPlayerForTeam<br>
  FROM player_mast<br>
  GROUP BY(playing_club))<br>
ORDER BY NumberOfPlayerForTeam DESC LIMIT 1;

If you want the playing clubs that have the most rows in your table, you can use rank() :如果您想要表格中行数最多的俱乐部,您可以使用rank()

SELECT pm.*
FROM (SELECT playing_club, COUNT(*) AS NumberOfPlayerForTeam,
             RANK() OVER (ORDER BY COUNT(*) DESC) as seqnum
      FROM player_mast
      GROUP BY playing_club
     ) pm
WHERE seqnum = 1;

Note:笔记:

  • COUNT(<column name>) counts the number of non- NULL values in the column. COUNT(<column name>)计算COUNT(<column name>)中非NULL值的数量。 There is no need to do this additional check;不需要做这个额外的检查; COUNT(*) does what you want. COUNT(*)做你想要的。
  • Parentheses are not needed around the GROUP BY keys. GROUP BY键周围不需要括号。

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

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