复杂的MySQL查询,如何选择所有成分并获得订单所需的数量

时间:2014-06-27 03:59:24

标签: php mysql sql

我有这个问题,我想列出手头的成分,以及任何未完成订单所需的成分数量。如果在订单中使用该成分,该查询可以正常工作,但如果它尚未在订单中使用,则它将不会在查询中显示该成分。

是否有办法构建此查询,以便显示所有成分和库存数量,以及任何未完成订单所需的金额?

建议表示赞赏。提前致谢。 (* 0.00220462将克数转换为磅数)

SELECT products.units, (orders_items.quantity - orders_items.quantity_baked) AS                                
num_loaved_needed, inventory.title, inventory.id, ROUND(inventory.quantity * 0.00220462,2)  
as pounds,
ROUND(SUM( (dough_recipes.grams / doughs.yield) * products.weight * (orders_items.quantity 
 - orders_items.quantity_baked) * 0.00220462 ),2) AS amount_needed_for_all_current_orders
          FROM orders_items 
          LEFT JOIN products ON products.id = orders_items.product_id
          LEFT JOIN doughs ON doughs.id = products.dough_id
          LEFT JOIN dough_recipes ON dough_recipes.dough_id = products.dough_id
          LEFT JOIN inventory ON inventory.id = dough_recipes.inventory_id
          GROUP BY inventory.id
          ORDER BY title ASC

0 个答案:

没有答案