[英]MYSQL Left Join with Subquery
我试图拉库存,并将所有项目转换为磅,然后加入我的面团配方,还将面团配方转换为磅的基本单位。 最终,我将在查询中对stocking_pounds和dough_recipe_ingredient_pounds进行计算,因此我需要将它们保留为子查询。 我无法弄清楚为什么这个查询将不起作用。 有什么建议或理由吗?
我看到的错误是:#1054-'on子句'中的未知列'dough_recipes.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
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
更新:我让整个查询都可以工作....但是变量return 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
查询的第二部分是创建一个名为oughing_recipe_ingredient_pounds的临时表,然后尝试将其与清单连接。 第二个表没有库存编号,这就是您收到错误的原因。 您的查询应为:
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
引擎抱怨,因为最后一个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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.