wHiTeHaT
wHiTeHaT

Reputation: 139

SQLite and If() condition

I have a MySQL query that I'm trying to run in SQLite. I found out that the IF condition isn't working in SQLite, and should be converted to a CASE. As the MySQL query is pretty big to have an overview, I was hoping someone can show me how it should be done. In one of many other MySQL queries, I must convert to SQLite.

Once I see how it should be done in a query that I use (because I'm familiar with it), I assume I can handle it for the others. Here is the MySQL that should run in SQLite:

select 
p.products_model, 
pd.products_name, 
m.manufacturers_name, 
p.products_quantity, 
p.products_weight, 
p.products_image, 
p.products_id, 
p.manufacturers_id, 
p.products_price, 
p.products_tax_class_id, 
IF(s.status, s.specials_new_products_price, NULL) as specials_new_products_price,
IF(s.status, s.specials_new_products_price, p.products_price) as final_price 
from 
products_description pd, 
products p 
left join manufacturers m on p.manufacturers_id = m.manufacturers_id 
left join specials s on p.products_id = s.products_id, 
products_to_categories p2c 
where 
p.products_status = "1" and 
p.products_id = p2c.products_id and 
pd.products_id = p2c.products_id and 
pd.language_id = "1" and 
p2c.categories_id = "10"

Upvotes: 3

Views: 1206

Answers (1)

mal-wan
mal-wan

Reputation: 4476

Change:

IF(s.status, s.specials_new_products_price, NULL) as specials_new_products_price,
IF(s.status, s.specials_new_products_price, p.products_price) as final_price 

to

CASE
    WHEN s.status <> 0 AND s.status IS NOT NULL
    THEN s.specials_new_products_price
    ELSE NULL
END AS specials_new_products_price,
CASE
    WHEN s.status <> 0 AND s.status IS NOT NULL
    THEN s.specials_new_products_price
    ELSE p.products_price
END AS final_price

Upvotes: 7

Related Questions