我试图拉动库存并将所有物品转换成磅,然后离开加入我的面团配方,并将面团配方转换为磅的基本单位。最后,我将在inventory_pounds和dough_recipe_ingredient_pounds的查询中运行计算,因此他们需要保留为我认为的子查询。我无法弄清楚为什么这个查询不起作用。有什么建议或理由吗?
我看到的错误是:#1054 - 未知栏' dough_recipes.inventory_id'在' on条款'
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
更新:我让整个查询工作....但变量返回AS不能像我想的那样在计算中工作。有没有办法让他们工作?
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
这是添加到查询时无效的计算:
dough_recipe_ingredient_pounds / dough_recipe_yield_pounds * products.weight *
(orders_items.quantity - orders_items.quantity_baked) AS
amount_needed_for_all_current_orders
答案 0 :(得分:1)
查询的第二部分是创建一个名为dough_recipe_ingredient_pounds的临时表,并尝试将其与库存一起加入。第二个表没有inventory_id,这就是您收到错误的原因。您的查询应该是:
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 FROM dough_recipes ) AS dough_recipe_ingredient_pounds
FROM inventory
LEFT JOIN dough_recipes
ON inventory.id = dough_recipes.inventory_id
答案 1 :(得分:0)
引擎抱怨是因为上一个ON
指的是dough_recipes
,这在外层看不到。
将最后一部分更改为
...
LEFT JOIN
(SELECT inventory_id,
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_recipe_ingredient_pounds.inventory_id