[英]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![]() |
country_id ![]() |
jersey_no![]() |
player_name![]() |
posi_to_play ![]() |
dt_of_bir ![]() |
age![]() |
playing_club ![]() |
---|---|---|---|---|---|---|---|
1231 ![]() |
1231 ![]() |
10 ![]() |
Hazard![]() |
striker![]() |
2/3/1991 ![]() |
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(*)
做你想要的。GROUP BY
keys. GROUP BY
键周围不需要括号。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.