sql - Nested Query Variable -


lets have database has table has column of countries (countries), table has column states , column country state in (states), , table has column cities , column state city in (cities). want write query outputs country, state in country, , how many cities in state

usa     ohio    65 usa     maine   52            etc..... 

my outer query like

select countries.country, states.state, count(cities.city) countries, states, cities countries.country = states.country ,        states.city = cities.city ,        exists... 

in exists statement want write nested subquery pull of cities out particular state count function count. there way me write variable or similar pull out cities state?

sorry if confusing example, actual query trying write more difficult explain. (using example) subquery pulling out every city in every single state. need change because if trying count number of cities in arizona, don't want seattle or de moines popping up.

select country, state, count(city) citycount countries inner join states on countries.country = states.country inner join cities on states.state = cities.state group country, state 

Comments

Popular posts from this blog

curl - PHP fsockopen help required -

HTTP/1.0 407 Proxy Authentication Required PHP -

c# - Resource not found error -