I am trying to pull the inventory and convert all items to pounds, and then left join my dough recipes and also convert the dough recipes to a base unit of pounds. Eventually I will be running a calculation within the query on inventory_pounds and dough_recipe_ingredient_pounds so they need to remain as sub-queries I believe. I cannot figure out why this query will not work. Any suggestions or rationale?
The error I see is: #1054 - Unknown column 'dough_recipes.inventory_id' in 'on clause'
SELECT inventory.id, inventory.title,
(SELECT
CASE inventory.units
WHEN 'kilograms'
THEN 2.20462 * inventory.quantity
WHEN 'pounds'
THEN 1 * inventory.quantity
WHEN 'ounces'
THEN 0.0625 * inventory.quantity
WHEN 'grams'
THEN 0.00220462 * inventory.quantity
END ) as inventory_pounds
FROM inventory
LEFT JOIN
(SELECT
CASE dough_recipes.units
WHEN 'kilograms'
THEN 2.20462 * dough_recipes.amount
WHEN 'pounds'
THEN 1 * dough_recipes.amount
WHEN 'ounces'
THEN 0.0625 * dough_recipes.amount
WHEN 'grams'
THEN 0.00220462 * dough_recipes.amount
END FROM dough_recipes ) AS dough_recipe_ingredient_pounds ON inventory.id =
dough_recipes.inventory_id
UPDATE: I got the entire query to work....but the variable return AS are not working in calculations like I was thinking they would. Is there a way to get them to work?
SELECT inventory.id, inventory.title,
(SELECT
CASE inventory.units
WHEN 'kilograms'
THEN 2.20462 * inventory.quantity
WHEN 'pounds'
THEN 1 * inventory.quantity
WHEN 'ounces'
THEN 0.0625 * inventory.quantity
WHEN 'grams'
THEN 0.00220462 * inventory.quantity
END ) as inventory_pounds,
(SELECT
CASE dough_recipes.units
WHEN 'kilograms'
THEN 2.20462 * dough_recipes.amount
WHEN 'pounds'
THEN 1 * dough_recipes.amount
WHEN 'ounces'
THEN 0.0625 * dough_recipes.amount
WHEN 'grams'
THEN 0.00220462 * dough_recipes.amount
END ) AS dough_recipe_ingredient_pounds,products.units,(orders_items.quantity -
orders_items.quantity_baked) AS num_loaved_needed,
( SELECT CASE doughs.units
WHEN 'kilograms'
THEN 2.20462 * doughs.yield
WHEN 'pounds'
THEN 1 * doughs.yield
WHEN 'ounces'
THEN 0.0625 * doughs.yield
WHEN 'grams'
THEN 0.00220462 * doughs.yield
END )
AS dough_recipe_yield_pounds
FROM inventory
LEFT JOIN dough_recipes ON inventory.id = dough_recipes.inventory_id
LEFT JOIN products ON dough_recipes.dough_id = products.dough_id
LEFT JOIN orders_items ON products.id = orders_items.product_id
LEFT JOIN doughs ON doughs.id = products.dough_id
This is the calculation the is not working when added to the query:
dough_recipe_ingredient_pounds / dough_recipe_yield_pounds * products.weight *
(orders_items.quantity - orders_items.quantity_baked) AS
amount_needed_for_all_current_orders