2011-12-06 9 views
2

私はシンプルMySQLは右

SELECT country.* , COUNT(country.coid) AS city_count_for_country 
FROM `air_countries` AS country 
JOIN `air_cities` AS city ON city.coid = country.coid 

を持っていますが、それは一つの国のためのすべての都市が、ちょうどすべての都市を数えていないだけで一つの国とcity_count_for_countryを返す動作していない参加します。どうしましたか?あなたはgroup byキーワードによってのみ達成可能である必要は何

SELECT country.* , COUNT(country.coid) AS city_count_for_country 
FROM `air_countries` AS country 
JOIN `air_cities` AS city ON city.coid = country.coid 
GROUP BY country.coid 

答えて

1

グループにそれを必要とする

おかげ

0

JOINだけでは行が集計されません。おそらく

SELECT country.* , COUNT(country.coid) AS city_count_for_country 
    FROM air_countries AS country 
    JOIN air_cities AS city 
    ON city.coid = country.coid 
    GROUP BY country.coid 
0

あなたはGROUP BY句が欠落しているクエリに

GROUP BY country.coid 
0

を追加することで、グループ機能を使用してみてくださいを書きたいです。試してみてください:

select country.coid, count(*) as city_count_for_country 
from `air_countries` as country 
join `air_cities` as city on city.coid = country.coid 
group by country.coid; 
関連する問題