Link to home
Start Free TrialLog in
Avatar of Steve Tinsley
Steve TinsleyFlag for United Kingdom of Great Britain and Northern Ireland

asked on

MySQL Query - Return 0 rather than NULL

I have a mysql query that returns the next number:

SELECT dispOrder+1 FROM (SELECT MAX(dispOrder) AS dispOrder FROM question) AS x

Open in new window


The problem is if all the numbers are NULL it returns NULL... How can I set it so that if all are NULL it returns 0 (+1)

?
ASKER CERTIFIED SOLUTION
Avatar of Tomas Helgi Johannsson
Tomas Helgi Johannsson
Flag of Iceland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Much depends you could use tomas' example and set the value to one without addition

Ifnull(column,1)

I find it difficult to answer a question or suggest an approach when a question has an approach that does not match the asker's need as a basis.

You have a select from a select.
Max(ifnull(column,1)) might be a way to limit nulls ......

As part of your query.
Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

Probably better to use an auto_increment column:

https://dev.mysql.com/doc/refman/8.0/en/example-auto-increment.html


You can have gaps with an auto_increment column but using your approach, you can have collisions.