如何在 MySQL WHERE 子句中使用别名列?

发布于 2024-07-12 22:50:36 字数 656 浏览 7 评论 0原文

我有一个像这样的 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 技术交流群。

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

发布评论

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

评论(5

猫腻 2024-07-19 22:50:37

啊,我已经找到地方了。

现在的声明是:

SELECT *, SUM(...some SQL removed for brevety) AS Occurrences FROM some_table AS q
WHERE criterion="value" GROUP BY q.P_id HAVING SUM(...some SQL removed for brevety)>0 ORDER BY Occurrences DESC LIMIT 10;

它似乎与 ORDER BY Occurrences 配合得很好,

Ah, I've found the place for it.

The statement is now:

SELECT *, SUM(...some SQL removed for brevety) AS Occurrences FROM some_table AS q
WHERE criterion="value" GROUP BY q.P_id HAVING SUM(...some SQL removed for brevety)>0 ORDER BY Occurrences DESC LIMIT 10;

It seems to be working fine with ORDER BY Occurrences,

浅唱ヾ落雨殇 2024-07-19 22:50:37

根据 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).

知你几分 2024-07-19 22:50:37

我认为聚合应该位于 HAVING 子句中。 不过,诚然,我不确定别名是否被接受。

HAVING (Occurrences > 0)

HAVING (SUM(...some SQL removed for brevety) > 0)

或者,您可以使用子查询:

SELECT *
FROM (
   -- your current query
) AS sub
WHERE (Occurences > 0)

I believe aggregates should be in the HAVING clause. Though, admittedly, I'm not sure if aliases are accepted.

HAVING (Occurrences > 0)

HAVING (SUM(...some SQL removed for brevety) > 0)

Alternatively, you can use a sub-query:

SELECT *
FROM (
   -- your current query
) AS sub
WHERE (Occurences > 0)
当爱已成负担 2024-07-19 22:50:36

我对 MySQL 不像对 SQL Server 那样熟悉,但在 T-SQL 中,不能在 GROUP BY 子句中使用别名(我最初也认为 ORDER BY 子句,但后来证明这是不正确的) 。 在本例中,您希望根据 GROUP BY 的结果进行过滤,因此我将使用 HAVING 子句,如下所示:

SELECT *, SUM(... some SQL removed for brevity ...) AS Occurrences
FROM
    some_table AS q
WHERE
    criterion = 'value'
GROUP BY
    q.P_id
HAVING
    SUM(... some SQL removed for brevity ...) > 0
ORDER BY
    Occurrences DESC
LIMIT 10;

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:

SELECT *, SUM(... some SQL removed for brevity ...) AS Occurrences
FROM
    some_table AS q
WHERE
    criterion = 'value'
GROUP BY
    q.P_id
HAVING
    SUM(... some SQL removed for brevity ...) > 0
ORDER BY
    Occurrences DESC
LIMIT 10;
撩心不撩汉 2024-07-19 22:50:36

这个查询对我有用......

select order_id, sum(qty_ordered) as total_qty 
from sales_flat_order_item 
group by order_id 
having total_qty > 10
order by total_qty desc

This query works for me...

select order_id, sum(qty_ordered) as total_qty 
from sales_flat_order_item 
group by order_id 
having total_qty > 10
order by total_qty desc
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文