attaboy Posted May 2, 2012 Share Posted May 2, 2012 this is my code: SELECT COUNT(*)AS num, country.Name FROM country, city WHERE CountryCode = Code GROUP BY country.Name HAVING num = MAX(num); it doesn't work if the last line is having num > 100 that works but I just want to show the highest number. Quote Link to comment Share on other sites More sharing options...
Jessica Posted May 2, 2012 Share Posted May 2, 2012 You need to write this in clear english I think, because you're selecting from two tables and never join them. It's not clear what you're attempting to do. It looks like you're trying to get the count of each country, and then get the maximum from that? AFAIK, you cannot reference a new column name (like num) in your statement, would have to do MAX(COUNT(*)) not MAX(num). But the entire thing looks wrong. Please explain what you're attempting to do, and figure out how to join your tables. Quote Link to comment Share on other sites More sharing options...
attaboy Posted May 2, 2012 Author Share Posted May 2, 2012 Thanks for the reply. I'm pretty sure it's a legitimate join and the alias is a real alias because it works and this comes straight from joins section of a book published by MySQL Press. I'm trying to get the country with the highest number of cities. SELECT COUNT(*)AS num, country.Name FROM country, city WHERE CountryCode = Code GROUP BY country.Name HAVING num > 200; Quote Link to comment Share on other sites More sharing options...
Jessica Posted May 2, 2012 Share Posted May 2, 2012 So now you're trying to select the max of num, which would be China in your example correct? Try: SELECT COUNT(*) AS num, country.Name FROM country, city WHERE CountryCode = Code GROUP BY country.Name ORDER BY count(*) DESC LIMIT 1 Quote Link to comment Share on other sites More sharing options...
attaboy Posted May 2, 2012 Author Share Posted May 2, 2012 Thank You so much! That did it. btw ORDER by num DESC also works. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.