MySql:如果子查询首先使用 min() 或 max() 选择列,如何按名称选择列?

发布于 2024-12-17 04:00:26 字数 691 浏览 1 评论 0原文

select position from 
    (select prefix, min(position) 
    from Chart_Position 
    group by chartnum) as Foo;

给出:

错误 1054 (42S22):“字段列表”中的未知列“位置”

显示以下列:

+----------+---------------+ 
| prefix   | min(position) |
+----------+---------------+ 
| 1964_232 |            87 | [rows truncated]

我正在尝试选择标题为“min(”上方的列位置)”但我不知道如何引用它。看来 min() 给我带来了麻烦。

感谢您的浏览!

编辑:找到它!我需要在子查询中为 min(position) 的结果起别名。就像这样:

select position from 
    (select prefix, min(position) as position
    from Chart_Position 
    group by chartnum) as Foo;
select position from 
    (select prefix, min(position) 
    from Chart_Position 
    group by chartnum) as Foo;

gives:

ERROR 1054 (42S22): Unknown column 'position' in 'field list'

when I select * from ... The following columns are shown:

+----------+---------------+ 
| prefix   | min(position) |
+----------+---------------+ 
| 1964_232 |            87 | [rows truncated]

I'm trying to select the column titled above "min(position)" but I can't figure out how to reference it. It seems like the min() is causing my trouble.

Thank you for taking a look!

EDIT: FOUND IT! I needed to alias the result of min(position) in the subquery. Like so:

select position from 
    (select prefix, min(position) as position
    from Chart_Position 
    group by chartnum) as Foo;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

谁与争疯 2024-12-24 04:00:26

你找到了;您可以在派生表子查询中为表达式列定义别名,然后可以在外部查询中通过该别名引用它们。

You found it; you can use define an alias for an expression column in the derived table subquery, then you can reference them by that alias in the outer query.

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