I have a mysql select query like this:
select r.restaurant_id, r.restaurant_name, r.city_id, c.name
from restaurants r
inner join cities c on c.id = r.city_id;
This is the result of above query:
+---------------+----------------------+---------+-------------+
| restaurant_id | restaurant_name | city_id | name |
+---------------+----------------------+---------+-------------+
| 7 | Somasiri Bake House | 5 | Mumbai |
| 8 | Indian Bake House | 7 | Chennai |
| 9 | KFC Rest | 5 | Mumbai |
| 10 | Indian t | 5 | Mumbai |
+---------------+----------------------+---------+-------------+
Now I want to display all the available cities with the number of restaurants existing to one city.
Eg: Mumbai (3), Chennai(1) and so on
I tried it like below with mysql COUN(), but it doesn't work for me.
SELECT c.name, count(r.city_id) AS count
FROM cities c
INNER JOIN restaurants r ON c.id = r.city_id;
Can anybody tell me what is the wrong with this?
Hope somebody may help me out. Thank you.
group by.