[英]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.