MySQL query order if null

I am trying to build a MySQL query that has order by expression. This is what I am trying to do:

SELECT * FROM tbl_product ORDER BY retail_price ONLY IF wholesale_price IS NULL OTHERWISE ORDER BY wholesale_price. 

I don’t know where to start. I found an article that uses ORDER BY COALESCE, but I also found that this might have performance issues.

Any advice is appreciated.

+7
source share
1 answer
 SELECT * FROM tbl_product ORDER BY ifnull(wholesale_price, retail_price); 

Please note that you do not need to select the value that you order - it can be any expression

+22
source

All Articles