mohammad zahedi mohammad zahedi - 5 months ago 42
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 


`sumFieldPrice` IS NOT NULL

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download