Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

return value 'no row' if max query return a NULL

Tags:

null

mysql

max

I have a table. From that I need to return Max value of a field. If that row is empty or if it doest satisfy some conditions the query will return NULL. I need to return NO ROWS if the max returns a NULL. How can i do it? I have tried IS NULL, IF,COALESCE But I am not able to get what i need.

Sorry, it was my mistake. I dont mean to say the word 'NO ROWS', More clearly i dont want to get any rows(no rows should be returned) if it returns NULL. I apologies again for my mistake.

like image 877
arjuncc Avatar asked Jan 04 '13 09:01

arjuncc


People also ask

Does Max function return null?

MAX returns NULL when there is no row to select. For character columns, MAX finds the highest value in the collating sequence. MAX is a deterministic function when used without the OVER and ORDER BY clauses.

How do you return null if there is no records found SQL?

Well by definition you can't return anything if there are no records. You would have to force the query to always return a resultset. So you can force the issue but it seems this type of thing is more suited to the front end instead of trying to make sql return data when there is no data to return.

How do you avoid NULL values in Max function?

You can use COALESCE() along with aggregate function MAX() for this.

How do I use Max Isnull in SQL?

ISNULL is an SQL function that replaces NULL values with a specified value. In this case, if the MAX value of your field is null, it replaces it with zero, otherwise it returns the MAX value of your selected field name. MAX is a standard SQL function that (unsurprisingly) returns the largest value in the set.


1 Answers

You have to use HAVING, like this:

SELECT MAX(field)
FROM yourtable
-- GROUP BY somefileds
HAVING MAX(field) is not null

HAVING specifies a condition for values of the aggregated columns.

like image 186
fthiella Avatar answered Sep 27 '22 18:09

fthiella