MySQL 别名问题

发布于 2024-08-03 21:48:13 字数 347 浏览 8 评论 0原文

我想知道为什么这会失败,

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

濫情▎り 2024-08-10 21:48:14

为了执行相关子查询,您需要外部的别名。您为外部表的字段创建了别名。看一下下面更正后的代码,它具有子查询中引用的表 (Cou) 的别名(请注意,字段别名不是必需的,因此我将其删除。如果您愿意,将其添加回来也没有什么坏处) :

SELECT Continent, Name, SurfaceArea
FROM Country Cou
WHERE SurfaceArea = 
(
    SELECT MAX(SurfaceArea)
    FROM Country
    WHERE Continent = Cou.Continent
);

关于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):

SELECT Continent, Name, SurfaceArea
FROM Country Cou
WHERE SurfaceArea = 
(
    SELECT MAX(SurfaceArea)
    FROM Country
    WHERE Continent = Cou.Continent
);

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.

韵柒 2024-08-10 21:48:14

我假设这与你的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文