MySql:如果子查询首先使用 min() 或 max() 选择列,如何按名称选择列?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你找到了;您可以在派生表子查询中为表达式列定义别名,然后可以在外部查询中通过该别名引用它们。
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.