简体   繁体   English

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

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

Here is my database:这是我的数据库:

在此处输入图片说明

I have problem with query, that probably is classic issue in ecommerce.我有查询问题,这可能是电子商务中的经典问题。

I have product that have ingredients, I am selling ingredients of products but what users are adding to cart are products.我有含有成分的产品,我在销售产品的成分,但用户添加到购物车的是产品。 so ie 10 products they add to cart, but in cart summary I need to display to them how much ingredients they are buying to create that products.因此,即他们添加到购物车的 10 种产品,但在购物车摘要中,我需要向他们显示他们购买了多少成分来创建该产品。

ie. IE。 people are buying ingredients to create 5 bottles of coca cola and 5 bottles of Sprite, and that is what they are adding to cart, system needs to calculate how much total sugar, water, cocacola_ingredient, sprite_ingredient need to be used produce those 5 bottles of coca cola and 5 bottles of sprite人们正在购买原料来制作 5 瓶可口可乐和 5 瓶雪碧,这就是他们添加到购物车的内容,系统需要计算需要使用多少总糖、水、cocacola_ingredient、sprite_ingredient 来生产这 5 瓶可口可乐和 5 瓶雪碧

i have table orderhasproducts that keeps info how much bottle of coca cola and sprite user ordered我有表orderhasproducts用户订购了多少瓶可口可乐和雪碧的信息

and I have table producthasingredients keeping info about how much particular ingredient are in particular product我有表producthasingredients保存有关特定产品中有多少特定成分的信息

what i need to do is calculate total number of all ingredients for all products in particular order.我需要做的是按特定顺序计算所有产品的所有成分的总数。

that is my last query, that still doesnt work:这是我的最后一个查询,仍然不起作用:

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

here are some more example info:以下是更多示例信息:

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

product.id=100    
product.quantity = 5;

orderhasproducts.id_product = 100;
orderhasproducts.numofproducts = 5

That means that product 100 has 5 Liters and has 10% of ingredient 200. In order we have 5 products (id 100)这意味着产品 100 有 5 升,并且有 10% 的成分 200。为了我们有 5 个产品 (id 100)

More sample data:更多示例数据:

lets say we have 2 products in database cocacola and sprite cocacola ingredients are: sugar (10%), water(80%), cocacolaingredient(10%) and bottle has 1 Liter.假设我们在数据库 cocacola 和 sprite cocacola 成分中有 2 种产品是:糖(10%)、水(80%)、cocacolaingredient(10%)和瓶子有 1 升。

sprite ingredients are: sugar (20%), water(70%), spritegredient(10%) and bottle has 1 Liter.雪碧成分是:糖(20%)、水(70%)、雪碧成分(10%)和瓶子有1升。

user place order: 10 bottles of cola, 10 bottles of sprite query should produce (in yellow): please check that image用户下单:10瓶可乐,10瓶雪碧查询应生产(黄色):请查看图片

UPDATE更新

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

gives me output给我输出

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        

now what I need to do is to sum ingredients with the same ID现在我需要做的是将具有相同 ID 的成分相加

This should give you the total for each ingredient.这应该给你每种成分的总数。 I added a division by 1 to convert to decimal beforehand or you'll lose info when dividing by 100:我预先添加了除以 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;

try this尝试这个

    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