小编wHi*_*HaT的帖子

SQLite和If()条件

我有一个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"
Run Code Online (Sandbox Code Playgroud)

mysql sqlite if-statement case

2
推荐指数
1
解决办法
867
查看次数

在SELECT查询中连接两个表不起作用

我经营一个购物车,产品可以有其他选择.例如,product1可以有多个选项,例如:

  • 尺寸:s,m,l,xl

  • 颜色:红色,蓝色,绿色

默认产品存储在customers_basket.

其他选择的项目存储在customers_basket_attributes.

在这两个表中,我插入了一个名为的唯一键products_unique_id.

这是为了识别客户何时添加具有相同选项的另一产品,如果是,我只需要更新customers_basket表的数量字段.

如果用户插入相同的产品但具有不同的选项,则products_unique_id更改.

以下查询不起作用:

SELECT * FROM 
customers_basket_attributes cba, 
customers_basket cb

WHERE 

cba.products_id = cb.products_id 
AND 
cba.products_unique_id = cb.products_unique_id 
AND customers_id="1"
Run Code Online (Sandbox Code Playgroud)

但是,此查询有效:

SELECT * FROM 
customers_basket cb, 
products p, 
products_description pd 
WHERE 
cb.products_id = p.products_id 
AND 
cb.products_id = pd.products_id 
AND customers_id="1"
Run Code Online (Sandbox Code Playgroud)

这也有效:

SELECT * FROM 
customers_basket_attributes cba, 
products p, 
products_description pd 
WHERE 
cba.products_id = p.products_id 
AND 
cba.products_id = pd.products_id 
AND customers_id="1"
Run Code Online (Sandbox Code Playgroud)

数据库结构和记录

以下是测试上述查询所需的数据库结构和记录: …

sql sqlite count

-4
推荐指数
1
解决办法
561
查看次数

标签 统计

sqlite ×2

case ×1

count ×1

if-statement ×1

mysql ×1

sql ×1