Skip to content
Advertisement

How to order by maximum of two column which can be null in MySQL?

I want to do something like :

maxof(Null,1000) should be 1000,

How to implement the maxof?

Advertisement

Answer

If you know that salaryplus will always be greater than salaryminus, then you can do

coalesce will return the first value which is not null, or (in this example) 0, if both values are null.

Otherwise, do something like this:

This will treat both salaryminus and salaryplus as 0 if they are null, and will order by the larger of the two.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement