COUNT(*)是否總是返回一個結果?

[英]Does COUNT(*) always return a result?


If I run a query such as:

如果我運行這樣的查詢:

SELECT COUNT(*) as num FROM table WHERE x = 'y'

Will it always return a result, even when the query doesn't match any record? Or do I need to validate and make sure a row is returned as the result?

它是否總是返回一個結果,即使查詢與任何記錄不匹配?還是需要驗證並確保結果返回一行?

7 个解决方案

#1


44  

Yes, because it's an aggregate and returns zero. Unless you add GROUP BY in which case no result because there is no group...

是的,因為它是一個集合,返回0。除非你添加組,在這種情況下沒有結果,因為沒有組…

MAX/SUM etc would return NULL unless you add GROUP BY then no rows. Only COUNT returns a number for no results

MAX/SUM等會返回NULL,除非你添加組,否則沒有行。只有COUNT返回一個沒有結果的數字

Edit, a bit late: SUM would return NULL like MAX

編輯,有點晚:SUM將返回NULL,就像MAX一樣

Edit, May 2013: this applies to all main RDBMS. I guess as per the ANSI standard

編輯,2013年5月:這適用於所有主要的RDBMS。我猜是按照ANSI標准

#2


8  

Yes, the return value of the "count" function itself is always a non-null integer, without exception.

是的,“count”函數本身的返回值總是一個非空整數,沒有例外。

Having said that, you may be more interested in the return value of your query, which is actually a result set. If that's the case, then you simply have to consider whether your query includes a "group by" clause.

話雖如此,您可能對查詢的返回值更感興趣,它實際上是一個結果集。

A non-grouped query with aggregate functions like

具有聚合函數的非分組查詢

select count(*), sum(*), max(*), min(*) from table

produces a result set with a single record containing the return value(s) of the aggregate function(s). In this case, your result set will always have exactly one record, with columns containing the return values of whatever aggregate functions you included. Within this lone record, the return value of count will always be a non-null integer, while the return values of the other aggregate functions such as min, max, and sum may be null. In the above example, your returned record may be "zero, null, null, null", but will never be "null,null,null,null", since count never returns null. So if you're calling only "count", then that one column in that one record returned will always contain the non-null return value of count; hence you can rest assured that the scalar return value of your count query will always be a non-null integer.

生成一個結果集,其中包含聚合函數的返回值。在這種情況下,您的結果集總是只有一個記錄,包含包含您所包含的任何聚合函數的返回值的列。在這個唯一的記錄中,count的返回值總是一個非空整數,而其他聚合函數(如min、max和sum)的返回值可能為空。在上面的示例中,返回的記錄可能是“0、null、null、null”,但是不會是“null、null、null、null”,因為count從不返回null。因此,如果只調用“count”,那么返回的一個記錄中的一個列將始終包含count的非空返回值;因此您可以放心,count查詢的標量返回值始終是一個非空整數。

On the other hand, a grouped query with aggregate functions like

另一方面,具有聚合函數的分組查詢

select count(*), sum(*), max(*), min(*) from table group by column

produces a result set with zero or more records where each record contains the return value of the aggregate function(s) for each group identified. In this case, if there are no groups identified, there are no groups for the aggregate functions to run on, the functions are never run, and the "query return value" will be an empty set with no records at all.

生成一個結果集,其中每個記錄包含標識的每個組的聚合函數的返回值。在這種情況下,如果沒有標識的組,則沒有用於運行聚合函數的組,也不會運行函數,並且“查詢返回值”將是一個空集,根本沒有記錄。

Test it out on a table with no records to see the results:

在沒有記錄的桌子上進行測試,看看結果如何:

create table #t (c int);
select COUNT(c), MIN(c), MAX(c), SUM(c) from #t; --returns one record {0, null, null, null}
select COUNT(c), MIN(c), MAX(c), SUM(c) from #t group by c; --returns no records
drop table #t;

So whether you're calling count, min, max, or sum, it is important to understand not only the return values of individual aggregate functions, but also the "return value" of the query itself, and what that result set contains.

因此,無論您是調用count、min、max還是sum,重要的是不僅要理解單個聚合函數的返回值,還要理解查詢本身的“返回值”以及查詢結果集包含的內容。

#3


5  

Aggregate function Count() always returns value

聚合函數Count()總是返回值

#4


2  

Yes it will return a numeric always

是的,它總是返回一個數字

#5


2  

Yes, it'll return 0 in such cases.

是的,在這種情況下它會返回0。

#6


2  

There will always be a row of result like this:

總會有一排這樣的結果:

| COUNT(*) |
------------
| 0        |

if there are no matches.

如果沒有匹配。

By the way, I would prefer to count only the primary key instead of *.

順便說一下,我寧願只計算主鍵而不計算*。

#7


2  

if no record is matched the count will return 0. (so yes, count always returns a result, unless you have some syntax error)

如果沒有匹配的記錄,計數將返回0。(是的,count總是返回一個結果,除非有語法錯誤)


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2010/03/31/7714a6587968a18df46ea02e275d266d.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com