SQLite和If()条件

时间:2011-09-06 22:08:43

标签: mysql sqlite if-statement case

我有一个MySQL查询,我试图在SQLite中运行。 我发现IF条件在SQLite中不起作用,应该转换为CASE。 由于MySQL查询非常重要,因此我希望有人可以告诉我应该如何完成。在许多其他MySQL查询中,我必须转换为SQLite。

一旦我看到它应该在我使用的查询中完成(因为我熟悉它),我认为我可以为其他人处理它。 这是应该在SQLite中运行的MySQL:

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"

1 个答案:

答案 0 :(得分:6)

变化:

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 

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