Skip to content
Advertisement

MySQL alternative way to `CASE` Statement

May I know, is there any alternative way or short way to write this case statement shown in the below:

I tried using mysql built in funciton ifnull as below:

ifnull(p.our_price,p.sales_price)

But it doesn’t work for me.

Advertisement

Answer

You could stick with a CASE expression but use COALESCE to make it more succinct:

Or, using the IF() function:

As a long time MySQL user, I often find myself using CASE more than IF() as the former allows omitting the ELSE condition entirely. IF() on the other hand always requires an explicit else value.

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