attaboy Posted May 6, 2012 Share Posted May 6, 2012 This doesn't work on my system mysql 5.5.16 ERROR 1054(42s22): unknown column 'c' in 'where clause' SELECT Code c, Name FROM Country WHERE Continent = 'Europe' AND EXISTS (SELECT * FROM CountryLanguage WHERE CountryCode = c AND Language = 'Spanish'); The error message states there's an unknown column 'c' in the where clause. can't subqueries handle aliases? Quote Link to comment https://forums.phpfreaks.com/topic/262162-problem-with-alias-in-subquery/ Share on other sites More sharing options...
gizmola Posted May 6, 2012 Share Posted May 6, 2012 Yes they can. You have a syntax issue. As best as I can tell, all you really want in the subquery is: SELECT * FROM CountryLanguage WHERE Language = 'Spanish' Quote Link to comment https://forums.phpfreaks.com/topic/262162-problem-with-alias-in-subquery/#findComment-1343529 Share on other sites More sharing options...
attaboy Posted May 6, 2012 Author Share Posted May 6, 2012 thanks for answering but that only gives me all the countries in Europe. Quote Link to comment https://forums.phpfreaks.com/topic/262162-problem-with-alias-in-subquery/#findComment-1343538 Share on other sites More sharing options...
gizmola Posted May 6, 2012 Share Posted May 6, 2012 That is because your WHERE clause in the outer query is restricing them: WHERE Continent = 'Europe' Quote Link to comment https://forums.phpfreaks.com/topic/262162-problem-with-alias-in-subquery/#findComment-1343539 Share on other sites More sharing options...
attaboy Posted May 6, 2012 Author Share Posted May 6, 2012 But the whole idea is to find only countries in Europe where Spanish is spoken. The original code I posted here is copied from the sample code of my textbook which is MySQL 5.0 Study Guide from MySQL Press I'm trying to determine if the book is wrong and you can't access an alias defined in an outer query from an inner query or if just I can't do that. Except the alias issue the query works fine. If in the inner query I replace the 'c' with 'country.Code' it works. Quote Link to comment https://forums.phpfreaks.com/topic/262162-problem-with-alias-in-subquery/#findComment-1343547 Share on other sites More sharing options...
attaboy Posted May 6, 2012 Author Share Posted May 6, 2012 I talked with my instructor and apparently the book is in error and it's a scope issue. Quote Link to comment https://forums.phpfreaks.com/topic/262162-problem-with-alias-in-subquery/#findComment-1343553 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.