这个 MySQL 查询有什么问题? SELECT * AS `x`,稍后如何再次使用x?
以下 MySQL 查询:
select `userID` as uID,
(select `siteID` from `users` where `userID` = uID) as `sID`,
from `actions`
where `sID` in (select `siteID` from `sites` where `foo` = "bar")
order by `timestamp` desc limit 100
...返回错误:
Unknown column 'sID' in 'IN/ALL/ANY subquery'
我不明白我在这里做错了什么。 sID
东西不应该是一列,而是我通过执行 (select siteID from users where userID = uID) as sID< 创建的“别名”(这叫什么?) /代码>。它甚至不在
IN
子查询内。
有什么想法吗?
编辑: @Roland:感谢您的评论。我有三个表:actions
、users
和 sites
。表 actions
包含一个 userID
字段,该字段对应于 users
表中的条目。此表中的每个用户 (users
) 都有一个 siteID
。 我尝试从 actions
表中选择最新操作,并将它们链接到 users
和 sites
表以找出执行这些操作的人员操作以及在哪个网站上进行。希望这是有道理的:)
The following MySQL query:
select `userID` as uID,
(select `siteID` from `users` where `userID` = uID) as `sID`,
from `actions`
where `sID` in (select `siteID` from `sites` where `foo` = "bar")
order by `timestamp` desc limit 100
…returns an error:
Unknown column 'sID' in 'IN/ALL/ANY subquery'
I don't understand what I'm doing wrong here. The sID
thing is not supposed to be a column, but the 'alias' (what is this called?) I created by executing (select siteID from users where userID = uID) as sID
. And it’s not even inside the IN
subquery.
Any ideas?
Edit: @Roland: Thanks for your comment. I have three tables, actions
, users
and sites
. The table actions
contains a userID
field, which corresponds to an entry in the users
table. Every user in this table (users
) has a siteID
.
I'm trying to select the latest actions from the actions
table, and link them to the users
and sites
table to find out who performed those actions, and on which site. Hope that makes sense :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我不知道这是否不在11年前的SQL标准中,但我发现它是使用HAVING的最简单的方法:
I don't know whether this was not in the SQL standard 11 years ago, but I found it the easiest way to use HAVING:
您需要将其包含在子查询中:
,或者更好的是,将其重写为
JOIN
创建以下索引:
You either need to enclose it into a subquery:
, or, better, rewrite it as a
JOIN
Create the following indexes:
在查询处理器完成 Select 子句并构建第一个中间结果集之前,不会建立列别名,因此如果需要,只能在 group By 中引用它(因为 group By 子句对该中间结果集进行操作)不要以这种方式使用它,将别名放入子查询中,然后它将出现在子查询生成的结果集中,因此可供外部查询访问。为了说明
(这不是执行此查询的最简单方法,但它说明了如何从子查询建立和使用列别名)
The column alias is not established until the query processor finishes the Select clause, and buiulds the first intermediate result set, so it can only be referenced in a group By, (since the group By clause operates on that intermediate result set) if you want ot use it this way, puit the alias inside the sub-query, then it will be in the resultset generated by the subquery, and therefore accessible to the outer query. To illustrate
(This is not the simplest way to do this query but it illustrates how to establish and use a column alias from a subquery)
请尝试以下操作:
Try the following:
我认为错误的原因是别名不可用于 WHERE 指令,这就是我们有 HAVING 的原因。
尽管我也同意其他答案,但您的查询可以更好地结构化。
I think the reason for the error is that the alias isn't available to the WHERE instruction, which is why we have HAVING.
Though i also agree with the other answers that your query could be better structured.
尝试以下操作
如果您希望稍后使用选择部分中的字段,您可以尝试子选择
Try the following
If you wish to use a field from the select section later you can try a subselect