繁体   English   中英

MySQL:如何按最终顺序获取产品的成分数量

[英]MySQL: how to get number of ingreedients of products in final order

这是我的数据库:

在此处输入图片说明

我有查询问题,这可能是电子商务中的经典问题。

我有含有成分的产品,我在销售产品的成分,但用户添加到购物车的是产品。 因此,即他们添加到购物车的 10 种产品,但在购物车摘要中,我需要向他们显示他们购买了多少成分来创建该产品。

IE。 人们正在购买原料来制作 5 瓶可口可乐和 5 瓶雪碧,这就是他们添加到购物车的内容,系统需要计算需要使用多少总糖、水、cocacola_ingredient、sprite_ingredient 来生产这 5 瓶可口可乐和 5 瓶雪碧

我有表orderhasproducts用户订购了多少瓶可口可乐和雪碧的信息

我有表producthasingredients保存有关特定产品中有多少特定成分的信息

我需要做的是按特定顺序计算所有产品的所有成分的总数。

这是我的最后一个查询,仍然不起作用:

SELECT ingredients.name, 

(SELECT SUM(producthasingredients.quantity * product.quantity / 100 * orderhasproducts.numofproducts) 
        FROM producthasingredients INNER JOIN product ON product.id= producthasingredients.id_product 
        WHERE ingredients.id=producthasingredients.id_ingredient )AS totalIngredient

FROM ingredients 

INNER JOIN producthasingredients ON ingredients.id = producthasingredients.id_ingredient 
INNER JOIN product ON producthasingredients.id_product=product.id
INNER JOIN orderhasproducts ON product.id=orderhasproducts.id_product 
WHERE orderhasproducts.id_order=15 
ORDER BY ingredients.id

以下是更多示例信息:

producthasingredients.id_ingredient = 200;
producthasingredients.quantity = 10;
producthasingredients.id_product =100    

product.id=100    
product.quantity = 5;

orderhasproducts.id_product = 100;
orderhasproducts.numofproducts = 5

这意味着产品 100 有 5 升,并且有 10% 的成分 200。为了我们有 5 个产品 (id 100)

更多示例数据:

假设我们在数据库 cocacola 和 sprite cocacola 成分中有 2 种产品是:糖(10%)、水(80%)、cocacolaingredient(10%)和瓶子有 1 升。

雪碧成分是:糖(20%)、水(70%)、雪碧成分(10%)和瓶子有1升。

用户下单:10瓶可乐,10瓶雪碧查询应生产(黄色):请查看图片

更新

following query:

        SELECT ingredients.name AS ingredientName, 
ingredients.id AS ingredientId, 
product.id AS productId, product.name AS productName,

        (producthasingredients.quantity * product.quantity / 100) AS ingredientQuantity

FROM ingredients 

INNER JOIN producthasingredients ON ingredients.id = producthasingredients.id_ingredient 
INNER JOIN product ON product.id=producthasingredients.id_product 
INNER JOIN orderhasproducts WHERE id_order=16

给我输出

ingredientName                ingredientId  productId  productName        iQuant  
----------------------------  ------------  ---------  -----------------  ------
proszek do drewna                        1          4  płyn do drewna     3.00          
proszek do podłóg                        2          3  płyn do podłóg     2.50         
proszek do szyb                          3          2  płyn do szyb       1.00          
składnik uniwersalny                     4          1  płyn do naczyń     2.00          
składnik uniwersalny                     4          2  płyn do szyb       1.00          
składnik uniwersalny                     4          3  płyn do podłóg     1.00          
proszek do płynu do naczyń               5          1  płyn do naczyń     1.00          
składnik uniwersalny 2                   6          4  płyn do drewna     1.00          
składnik uniwersalny 3                   7          1  płyn do naczyń     0.50          
składnik uniwersalny 3                   7          2  płyn do szyb       0.50        

现在我需要做的是将具有相同 ID 的成分相加

这应该给你每种成分的总数。 我预先添加了除以 1 以转换为十进制,否则除以 100 时您将丢失信息:

SELECT distinct ingredients.name, 

(SELECT SUM((producthasingredients.quantity/1.0) * (product.quantity/1.0) / 100 * orderhasproducts.numofproducts)
        FROM producthasingredients INNER JOIN product ON product.id= producthasingredients.id_product 
        WHERE ingredients.id=producthasingredients.id_ingredient )AS totalIngredient

FROM ingredients 

INNER JOIN producthasingredients ON ingredients.id = producthasingredients.id_ingredient 
INNER JOIN product ON producthasingredients.id_product=product.id
INNER JOIN orderhasproducts ON product.id=orderhasproducts.id_product 
WHERE orderhasproducts.id_order=15
ORDER BY ingredients.id;

尝试这个

    SELECT result.name,SUM(result.totalIngredient) FROM 
(SELECT distinct ingredients.name, (SELECT SUM((producthasingredients.quantity/1.0) * (product.quantity/1.0) / 100 * orderhasproducts.numofproducts) FROM producthasingredients INNER JOIN product ON product.id= producthasingredients.id_product WHERE ingredients.id=producthasingredients.id_ingredient )AS totalIngredient FROM ingredients INNER JOIN producthasingredients ON ingredients.id = producthasingredients.id_ingredient INNER JOIN product ON producthasingredients.id_product=product.id INNER JOIN orderhasproducts ON product.id=orderhasproducts.id_product WHERE orderhasproducts.id_order=1 ORDER BY ingredients.id) as result 
GROUP BY name

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM