检查SQL MAX()函数是否返回null [英] Checking SQL MAX() Function returning null
问题描述
我的问题是,如果我的表为空,或者我正在使用max函数的列没有我指定的值,那么sqlDataReader.hasRows为什么为真?
My problem is if my table is empty or the column on which I am using max function does not have the value I have specified, then why is sqlDataReader.hasRows TRUE?
它提供了一个空记录...我该如何解决这个问题?
It is giving a null record...how do I solve this problem??
谢谢.
推荐答案
像MAX()这样的聚合函数将始终为每个组返回一行.在您的情况下,您的小组就是整个桌子.因此,您将获得具有MAX值的一行结果集.由于表中没有数据,因此MAX值是不确定的,因此为NULL.
An aggregate function like MAX() will always return one row per group. In your case, your group is the whole table. Therefore you get a one row resultset with the MAX value. Since you don't have data in your table, the MAX value is undefined, therefore NULL.
为更好地理解,请尝试将MAX替换为COUNT.这也将返回一行,但其值为0.我认为这更直观,并且可以帮助您更好地了解正在发生的事情.
For better understanding try to replace MAX with COUNT. This will also return one row but with value 0. I think this is more intuitive and will help you better to understand what is happening.
这篇关于检查SQL MAX()函数是否返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!