Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking SQL MAX() Function returning null

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??

Thanks in advance.

like image 254
user3714811 Avatar asked May 29 '26 21:05

user3714811


1 Answers

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.

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.

like image 123
Martin K. Avatar answered May 31 '26 11:05

Martin K.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!