mysql - SQLite 和 If() 条件

标签 mysql sqlite if-statement case

我有一个试图在 SQLite 中运行的 MySQL 查询。 我发现 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"

最佳答案

改变:

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

关于mysql - SQLite 和 If() 条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7326738/

相关文章:

php - 删除和更新 mysql 数据库

android - 外键问题

java - SQLite 数据库到数据类

if-statement - Cypress - 如果那么起作用

java - if语句中的代码突然离开

javascript - 强制文本框或变量的长度

php - 从完整的数据过滤器年份中获取年份

mysql - 使用mysql加载数据

java - 通过Thread关闭MySQL连接

c++ - 在C++中异步执行sql查询