2011-07-11 101 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條款