简体   繁体   English

如何计算具有HAVING准则的MySQL条目

[英]How do I count MySQL entries with a HAVING criterion

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 和COUNT(field)将返回符合标准field.id> 100的条目数

But then what if you what to count entries specified with the HAVING criterion such as 但是,如果要计算用HAVING准则指定的条目,该怎么办呢?

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? 我该如何对通过HAVING子句指定条件的条目进行计数?

Thanks in advance 提前致谢

Well, COUNT works BEFORE HAVING is applied to the result set . 好吧, COUNT HAVING 之前的作品将应用于结果集 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
)

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) ). 我不能按原样运行任何一个查询-它们给我一个1140错误,因为“使用没有GROUP BY子句的聚合”(即: 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. MySQL确实支持在GROUP BYHAVING子句中引用列别名。 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... 我不推荐这种方法-SQL Server支持该方法,但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. HAVING子句类似于WHERE子句,不同之处在于HAVING子句支持聚合函数,而无需将其包装在子查询中。

DISCLAIMER - I've only tested this on SQL Server 免责声明-我仅在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; 因为field.id不包含在定义组或聚合函数的子句中; it just doesn't compile. 它只是不编译。

With that said, the following SQL - 这样说,下面的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. 如果计数大于100,将返回表中的行 。如果不是,则不会有任何结果。

Do you have a specific problem in mind? 您有特定的想法吗? What are you trying to count? 你想算什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM