MySQL:如何获取最终订单中产品的Ingreedints数量
这是我的数据库: 我对查询有问题,这可能是电子商务中的经典问题 我有一种含有成分的产品, 我销售产品的成分,但用户添加到购物车的是产品。 例如,他们向购物车添加了10种产品,但在购物车摘要中,我需要向他们显示他们购买了多少成分来创建该产品 也就是说,人们购买配料来生产5瓶可口可乐和5瓶雪碧,这就是他们添加到购物车中的配料,系统需要计算生产这5瓶可口可乐和5瓶雪碧所需的总糖、水、可口可乐配料、雪碧配料 我有一个表格MySQL:如何获取最终订单中产品的Ingreedints数量,mysql,sql,Mysql,Sql,这是我的数据库: 我对查询有问题,这可能是电子商务中的经典问题 我有一种含有成分的产品, 我销售产品的成分,但用户添加到购物车的是产品。 例如,他们向购物车添加了10种产品,但在购物车摘要中,我需要向他们显示他们购买了多少成分来创建该产品 也就是说,人们购买配料来生产5瓶可口可乐和5瓶雪碧,这就是他们添加到购物车中的配料,系统需要计算生产这5瓶可口可乐和5瓶雪碧所需的总糖、水、可口可乐配料、雪碧配料 我有一个表格orderhasproducts,其中保存了用户订购了多少瓶可口可乐和雪碧的信息
orderhasproducts
,其中保存了用户订购了多少瓶可口可乐和雪碧的信息
我有一个表ProducthasComponents
保存了关于特定产品中有多少特定成分的信息
我需要做的是计算特定订单中所有产品的所有成分的总数
这是我的最后一个问题,仍然不起作用:
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
可口可乐的成分有:糖(10%)、水(80%)、可口可乐成分(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
请提供一些样本数据,基于样本数据的预期输出,以及您的查询在样本数据上产生的输出。请同时说明哪些不起作用,不要指望我们自己也能弄清楚。“计算特定订单中所有产品的所有成分总数”的措辞含糊不清。它可以是所有产品中所有成分的总和,也可以是每个产品中成分的数量。product.quantity在你的概念中是什么意思?product.quantity的意思是-即,在一瓶可乐中,你有1升可口可乐,我创建了excel并制作了屏幕截图,因此它应该会有所帮助。此结果错误:错误代码:1242子查询返回的rownope超过1个。。ProductHasComponents内部连接ProductHasComponents;)