Click here to Skip to main content
15,887,676 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello all,

I have a db that collects various countries and cities.

I'll like to export the data by Countries. That is I need a query that will select the distinct countries, and then also list all the cities of each country.

The query I have now is: SELECT distinct city FROM `data_table` GROUP BY location

Its not working though.

Kindly help. Thank you
Posted
Comments
Richard MacCutchan 6-Nov-14 7:31am    
What does "not working" mean? What is the structure of your table, what results do you expect, and what do you actually get?

No, that won't work, because City is not in the GROUP BY clause, and GROUP BY returns individual rows - so you can't select multiple rows out of that!
Instead try:
SQL
SELECT city FROM data_table GROUP BY location, city
 
Share this answer
 
Comments
namo77 11-Nov-14 10:30am    
Hi. Thank you all for your response.

I still haven't gotten it right. The closest i came was: <pre lang="sql">SELECT location, COUNT(location)
FROM (SELECT email, location FROM data_table)
AS result_one
GROUP BY location</pre>


This only brought the number of downloads from each country. It didn't bring out the number of cities, it didn't also list out the cities in the countries.

I want it to be listed out this way:
NIGERIA USA
abuja Florida
lagos Atlanta
PH Houston


Please help. Thanks
Quote:
The query I have now is: SELECT distinct city FROM `data_table` GROUP BY location



1) you haven't mentioned your Group by column in Your Select List
2)you don't need Group by Clause because you didn't have Any Aggregate function in your Select List

Change into

SQL
SELECT distinct city FROM `data_table` GROUP BY location,city

or 

SELECT distinct city FROM `data_table`
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900