编写sql查询的帮助
我正在尝试编写一个查询,以获取表中每个国家的运动队球员人数: 我有一个包含以下字段的表编写sql查询的帮助,sql,Sql,我正在尝试编写一个查询,以获取表中每个国家的运动队球员人数: 我有一个包含以下字段的表 country | sports_team | player_name 我想展示的是一个与此类似的reulst country | sports_team | number_of_players i、 我想环游所有国家,对于运动队的每一次变化,我想记录下队中球员的数量 样本表: country | sports_team | player_name c1 | teamA | nam
country | sports_team | player_name
我想展示的是一个与此类似的reulst
country | sports_team | number_of_players
i、 我想环游所有国家,对于运动队的每一次变化,我想记录下队中球员的数量
样本表:
country | sports_team | player_name
c1 | teamA | name1
c1 | teamA | name2
c1 | teamB | name3
c2 | teamC | name4
c2 | teamC | name5
样本结果
country | sports_team | number_of_players
c1 | teamA | 2
c1 | teamB | 1
c2 | teamC | 2
我刚开始编写sql查询,我不知道如何处理,如果有任何帮助,我将不胜感激 使用:
使用:
试试这个:
SELECT country, sports_team, COUNT(*) AS number_of_players
FROM yourtable
GROUP BY country, sports_team
ORDER BY country, sports_team;
SELECT
Country, Sports_Team, COUNT(player_name) AS number_of_players
FROM
YourTable
GROUP BY
Country, Sports_Team
像COUNT这样的查询是此类数据处理的关键。尝试以下方法:
SELECT country, sports_team, COUNT(*) AS number_of_players
FROM yourtable
GROUP BY country, sports_team
ORDER BY country, sports_team;
SELECT
Country, Sports_Team, COUNT(player_name) AS number_of_players
FROM
YourTable
GROUP BY
Country, Sports_Team
像COUNT这样的查询是此类数据处理的关键。尝试以下方法:
SELECT country, sports_team, COUNT(*) AS number_of_players
FROM yourtable
GROUP BY country, sports_team
ORDER BY country, sports_team;
SELECT
Country, Sports_Team, COUNT(player_name) AS number_of_players
FROM
YourTable
GROUP BY
Country, Sports_Team
试试这个:
SELECT country, sports_team, COUNT(*) AS number_of_players
FROM yourtable
GROUP BY country, sports_team
ORDER BY country, sports_team;
SELECT
Country, Sports_Team, COUNT(player_name) AS number_of_players
FROM
YourTable
GROUP BY
Country, Sports_Team
不需要循环,只需聚合
SELECT country, sports_team, COUNT(*)
FROM myTable
GROUP BY country, sports_team
神奇之处在于COUNT*和groupby子句不需要循环,只需聚合即可
SELECT country, sports_team, COUNT(*)
FROM myTable
GROUP BY country, sports_team
神奇之处在于COUNT*和GROUPBY子句尝试在SELECT语句中读取少量内容。这应该在第2页左右。试着在SELECT语句上读少量内容。这应该在第2页。