MySQL 别名问题
我想知道为什么这会失败,
mysql> SELECT Continent C, Name, SurfaceArea -> FROM Country -> WHERE SurfaceArea = ( -> SELECT MAX(SurfaceArea) -> FROM Country -> WHERE Continent = C); ERROR 1054 (42S22): Unknown column 'C' in 'where clause'
它是一些示例练习的认证指南提供的答案。
顺便说一句,对于别名,我什么时候必须使用 AS?是可选的吗?
i am wondering why this fails
mysql> SELECT Continent C, Name, SurfaceArea -> FROM Country -> WHERE SurfaceArea = ( -> SELECT MAX(SurfaceArea) -> FROM Country -> WHERE Continent = C); ERROR 1054 (42S22): Unknown column 'C' in 'where clause'
its a answer provided by the certification guide for some sample exercises.
btw, for alias when do i have to use AS? isit optional?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为了执行相关子查询,您需要外部表的别名。您为外部表的字段创建了别名。看一下下面更正后的代码,它具有子查询中引用的表 (Cou) 的别名(请注意,字段别名不是必需的,因此我将其删除。如果您愿意,将其添加回来也没有什么坏处) :
关于AS的使用,是可选的。例如,在上面的查询中,您可以编写
Country AS Cou
,它是相同的。In order to execute a correlated subquery, you need an alias for the outer table. You created an alias for the outer table's field. Take a look at the corrected code below, that has an alias for the table (Cou) that is referenced in the subquery (note that the field alias is not required so I removed it. There's no harm in adding it back if you wish):
Regarding the usage of AS, it's optional. For example, in the query above you could write
Country AS Cou
, it would be the same.我假设这与你的 MySQL 版本有关。我刚刚测试了完全相同相同的查询,它成功了(至少在语法上)。这是MySQL 5.0.45。
如果 MySQL 版本不是问题,您也可以尝试重新输入查询。这可能听起来很愚蠢,但有时在键入或复制/粘贴时,不可打印字符可能会滑入您的查询中(我想从这里的 PDF 中复制/粘贴,特别是因为它来自指南)。
是的,
AS
对于别名来说是可选的。I'm assuming it has something to do with you're MySQL version. I just tested exactly the same query and it succeeds (syntaxically at least). Thats with MySQL 5.0.45.
If the MySQL version is not the issue, you could also try retyping the query. It may sound silly but sometimes unprintable characters may slip in your query while typing or copy/pasting (I'm thinking copy/paste from a PDF here, especially, since it's from a guide).
And, yes,
AS
is optional for aliases.