2012-03-27 8 views
1

Ich habe zwei Tabellen in einer MySQL-Datenbank:Join Two Tables und dann Gruppen von

COUNTRY 
--------------- 
id, 
country_name 

und

CITY 
--------------- 
c_id, 
city_name, 
id 

Das Land ist eine übergeordnete Tabelle der Stadt: country.id = city.id

Ich möchte Folgendes anzeigen:

Country_name count of cities 
------------------------------ 
USA   333 
UAE   293 
.... 

* Alle anzeigen, wenn Land null oder Stadt null ist.

Antwort

1
SELECT 
    cn.country_name, 
    COUNT(DISTINCT c.c_id) AS count_of_cities 
FROM 
    COUNTRY cn LEFT JOIN 
    CITY c ON c.id = cn.id 
GROUP BY cn.country_name