如何在 MySQL WHERE 子句中使用别名列?
我有一个像这样的 MySQL 查询:
SELECT *, SUM(...some SQL removed for brevety) AS Occurrences FROM some_table AS q
WHERE criterion="value" GROUP BY q.P_id ORDER BY Occurrences DESC LIMIT 10;
我想将结果限制为出现次数 > 0 的行。 这对我来说似乎很简单,但我似乎无法让它发挥作用。 无论我尝试什么,无论是 WHERE 还是 HAVING,每当我尝试添加此限制时,我都不会得到任何 ROWS 回报。 我确信我的表中有应该返回的数据。 有谁知道如何完成我正在做的事情?
我已经尝试过这个,但它仍然不起作用。 有什么想法为什么它仍然不起作用吗?
SELECT *, SUM(...some SQL removed for brevety) AS Occurrences FROM some_table AS q
WHERE criterion="value" HAVING SUM(...some SQL removed for brevety)>0 GROUP BY q.P_id ORDER BY Occurrences DESC LIMIT 10;
I have a MySQL query like this:
SELECT *, SUM(...some SQL removed for brevety) AS Occurrences FROM some_table AS q
WHERE criterion="value" GROUP BY q.P_id ORDER BY Occurrences DESC LIMIT 10;
I want to restrict the results to rows where Occurrences>0. This seems very simple to me, but I can't seem to make it work. No matter what I try, WHEREs or HAVINGs, whenever I try to add this restriction I get NO ROWS in return. I'm positive that there is data in my table that should be returned. Does anyone know how to accomplish what I'm doing?
I've tried this, but it still doesn't work. Any ideas why it still won't work?
SELECT *, SUM(...some SQL removed for brevety) AS Occurrences FROM some_table AS q
WHERE criterion="value" HAVING SUM(...some SQL removed for brevety)>0 GROUP BY q.P_id ORDER BY Occurrences DESC LIMIT 10;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
啊,我已经找到地方了。
现在的声明是:
它似乎与 ORDER BY Occurrences 配合得很好,
Ah, I've found the place for it.
The statement is now:
It seems to be working fine with ORDER BY Occurrences,
根据 SQL 标准,列别名在查询正文中不可用,这是一个令人困惑的麻烦。 根据您的尝试,听起来 MySQL(以及其他一些 DBMS)也是如此。 您可能必须重复 ORDER BY 子句中的表达式。
此外,GROUP BY 子句应列出 * 中的每一列,而不仅仅是主键(尽管从逻辑上讲,指定主键就足够了)。
According to the SQL standard, the column aliases are not available in the body of the query, which is a confounded nuisance. Given what you have tried, it sounds as though the same is true of MySQL (and some other DBMS). You'll probably have to repeat the expression in the ORDER BY clause.
Also, the GROUP BY clause should list every column that is in the *, not just the primary key (though, logically, specifying the primary key should be sufficient).
我认为聚合应该位于
HAVING
子句中。 不过,诚然,我不确定别名是否被接受。或者,您可以使用子查询:
I believe aggregates should be in the
HAVING
clause. Though, admittedly, I'm not sure if aliases are accepted.Alternatively, you can use a sub-query:
我对 MySQL 不像对 SQL Server 那样熟悉,但在 T-SQL 中,不能在 GROUP BY 子句中使用别名(我最初也认为 ORDER BY 子句,但后来证明这是不正确的) 。 在本例中,您希望根据 GROUP BY 的结果进行过滤,因此我将使用 HAVING 子句,如下所示:
I am not as familiar with MySQL as I am with SQL Server, but in T-SQL, you can't use aliases in GROUP BY clauses (I originally thought ORDER BY clauses as well, but that has since been shown to be incorrect). In this case, you want to filter based on the results of a GROUP BY, so I would use a HAVING clause as follows:
这个查询对我有用......
This query works for me...