2011-07-11 134 views
1

我想编写一个查询,将在表中获取玩家的数量在一个运动队对每一个国家:帮助中写一个SQL查询

我有一个表具有以下字段

country | sports_team | player_name 

我要显示与此类似

country | sports_team | number_of_players 

即reulst我想遍历所有国家,以及在SPORTS_TEAM每一个变化,我想记录numm队中的球员。

样品表:

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查询,我不知道如何PROCEDE,任何帮助将不胜感激!

+1

尝试在SELECT语句读取只是一个小数目。这应该在第2页左右。 – Randy

回答

3

试试这个:

SELECT country, sports_team, COUNT(*) AS number_of_players 
FROM yourtable 
GROUP BY country, sports_team 
ORDER BY country, sports_team; 

Aggregate查询,如COUNT对于这种数据处理的关键。

3

使用GROUP BY

SELECT country, sports_team, COUNT(*) 
FROM whatever_your_table_is  
GROUP BY country, sports_team 
1

试试这个:

SELECT 
    Country, Sports_Team, COUNT(player_name) AS number_of_players 
FROM 
    YourTable 
GROUP BY 
    Country, Sports_Team 
1
select country, sports_team, count(*) from <your table> group by country, sports_team 
2
select 
    country, 
    sports_team, 
    count(*) number_of_players 
from table 
group by country, sports_team 
1

无需环路,只是汇总。

SELECT country, sports_team, COUNT(*) 
FROM myTable 
GROUP BY country, sports_team 

神奇的是在COUNT(*)GROUP BY条款