mohammad zahedi mohammad zahedi - 22 days ago 9
MySQL Question

use nested IF in select query mysql

How can I use nested IF in part of select in query MySQL?

I do this, but It doesn't work

this is the code:

SELECT *, IF(`view_price_trp` <= 0 ,IF(`view_price_sgl` > 0,
(((`tour_package`.`view_dbl` * 2) + (`tour_package`.`view_sgl`)+
(`tour_package`.`view_dbl` * 2)) + ((`tour_package`.`price_b`))), NULL) ,
(((`tour_package`.`view_trp` * 3)+(`tour_package`.`view_dbl` * 2)) +
((`tour_package`.`price_b`)))) As sumFieldPrice FROM `tbl_price` WHERE
((`tour_package`.`id` IN ('1420', '1421') HAVING (`sumFieldPrice` >= 10) AND
(`sumFieldPrice` <= 1900) AND (`sumFieldPrice` <> NULL)


I Have question how can I achieve NULL in this query
I use it in having like this : (sumPriceField <> NULL) but after that, there isn't any return value

Is these query true?
can we use nested if in MySQL?

Answer Source

You can't check NULL value by Operator. You can check it by using IS NULL in mysql. You need to replace

`sumFieldPrice` <> NULL 

with

`sumFieldPrice` IS NOT NULL

I am considering if condition is closed properly as it's not closed properly here.