如何使用 HAVING 标准对 MySQL 条目进行计数

发布于 2024-09-20 00:21:04 字数 386 浏览 11 评论 0原文

因此,通常您可以这样做

SELECT COUNT(field.id) FROM table WHERE field.id > 100

,并且 COUNT(field) 将返回符合 field.id > 条件的条目数。 100

但是,如果您要对使用 HAVING 条件指定的条目进行计数(例如

SELECT COUNT(field.id), field.id * 10 AS foo FROM table HAVING foo > 100

在本例中代码不起作用),该怎么办...

我该如何对通过 HAVING 子句指定的条件进行计数的条目进行计数?

提前致谢

So usually you can just do

SELECT COUNT(field.id) FROM table WHERE field.id > 100

and COUNT(field) will return the number of entries that has the criterion of field.id > 100

But then what if you what to count entries specified with the HAVING criterion such as

SELECT COUNT(field.id), field.id * 10 AS foo FROM table HAVING foo > 100

the code wouldn't work in this case....

How do I go about counting entries whose criterion are specified via the HAVING clause?

Thanks in advance

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

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

发布评论

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

评论(3

泛泛之交 2024-09-27 00:21:04

好吧,COUNT 的工作BEFORE HAVING 应用于结果集。因此,如果您需要计算它们的数量 - 您必须用另一个查询来包装您的查询。

SELECT COUNT(*) FROM (
    SELECT field.id * 10 AS foo FROM table HAVING foo > 100
)

Well, COUNT works BEFORE HAVING is applied to the result set. So if you need to count their number - you have to wrap your query with another one.

SELECT COUNT(*) FROM (
    SELECT field.id * 10 AS foo FROM table HAVING foo > 100
)
花期渐远 2024-09-27 00:21:04

我无法按原样运行任何一个查询 - 他们给我一个 1140 错误,提示“使用不带 GROUP BY 子句的聚合”(即:COUNT(field.id))。一切似乎都与聚合无关,只是能够引用列别名进行比较...

最广泛支持的方式是:

SELECT field.id * 10 AS foo 
  FROM table 
 WHERE field.id * 10 > 100

MySQL 确实支持在 GROUP BY 中引用列别名或HAVING 子句。它不需要使用反引号,但我见过在反引号出现之前无法工作的实例(非保留字):

SELECT field.id * 10 AS foo 
  FROM table 
HAVING `foo` > 100

我不推荐这种方法 - 它在 SQL Server 上受支持,但在 Oracle 上不受支持

... HAVING 子句类似于 WHERE 子句,不同之处在于 HAVING 子句支持聚合函数,而不需要将它们包装在子查询中。

I can't run either query as-is - they give me a 1140 error for "using an aggregate without a GROUP BY clause" (IE: COUNT(field.id)). Everything appears not to relate to the aggregate at all, just the ability to reference the column alias for comparison...

The most widely supported means is:

SELECT field.id * 10 AS foo 
  FROM table 
 WHERE field.id * 10 > 100

MySQL does support referencing a column alias in the GROUP BY or HAVING clause. It doesn't require using backticks, but I have seen instances that wouldn't work (non-reserved words) until backticks were present:

SELECT field.id * 10 AS foo 
  FROM table 
HAVING `foo` > 100

I don't recommend this approach - it's supported on SQL Server, but not Oracle...

The HAVING clause is like the WHERE clause, the difference is that the HAVING clause supports aggregate functions without needing them to be wrapped in a subquery.

生来就爱笑 2024-09-27 00:21:04

免责声明 - 我只在 SQL Server 上对此进行了测试,

在这种情况下,HAVING 只会对整个返回集执行任何聚合查询。首先,无法运行,

SELECT COUNT(field.id), field.id * 10 AS foo FROM table HAVING foo > 100

因为field.id不包含在定义组或聚合函数的子句中;它只是无法编译。

话虽如此,如果计数大于 100,以下 SQL -

SELECT COUNT(field.id) FROM table HAVING COUNT(field.id) > 100

将返回表中的行数。如果不是,则不会得到任何结果。

您有一个具体的问题吗?你想数什么?

DISCLAIMER - I've only tested this on SQL Server

HAVING in this case will only perform any aggregate queries over the entire returned set. First of all, you can't run

SELECT COUNT(field.id), field.id * 10 AS foo FROM table HAVING foo > 100

because field.id is not contained in a clause that defines a group or an aggregate function; it just doesn't compile.

With that said, the following SQL -

SELECT COUNT(field.id) FROM table HAVING COUNT(field.id) > 100

will return the count of rows in the table if the count is greater than 100. If it's not, you'll get no result.

Do you have a specific problem in mind? What are you trying to count?

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