Skip to content
Advertisement

Mysql where clause on multiple fields which one might be null

In my products table, I have 2 columns: price and discounted_price in which discounted_price is null most of the time unless there is a promotion for that product:

JavaScript

The product with id of 2 is now on promotion and has discounted_price. Now I want to query products to get products cheaper than 50 but in my query I need to check if discounted_price is null then look into price, otherwise look into discounted_price. What Google said is using:

JavaScript

But it’s not working 🙁

Advertisement

Answer

Alternative, you can use sub query, like this :

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