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.
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
i have table orderhasproducts
that keeps info how much bottle of coca cola and sprite user ordered
and I have table producthasingredients
keeping info about how much particular ingredient are in particular product
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)
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.
sprite ingredients are: sugar (20%), water(70%), spritegredient(10%) and bottle has 1 Liter.
user place order: 10 bottles of cola, 10 bottles of sprite query should produce (in yellow): please check that image
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
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:
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.