让我们说我经营一家弯曲的汽车公司。我们说我有下表:
car_engine_mileage_counters
这是car_engines
到mileage_counters
的联接表,同时还存储mileage
的计算字段
还可以说我在engine template
上在工厂的引擎块级别编码系数。
UPDATE car_engine_mileage_counters
SET mileage = mileage_counters.mileage * coefficients.coefficient
FROM car_engines
INNER JOIN engine_templates
ON car_engines.template_id = engine_templates.id
INNER JOIN mileage_counters
ON mileage_counters.id = car_engine_mileage_counters.mileage_counter_id
INNER JOIN mileage_counter_templates
ON mileage_counter.template_id = mileage_counter_templates.id
INNER JOIN coefficients
ON coefficients.mileage_counter_template_id = mileage_counter_templates.id
WHERE coefficients.engine_template_id = engine_template.id AND car_engines.id = car_engine_mileage_counters.engine_id;
这个(显然是虚构的)示例失败了:
错误:对表的FROM子句条目的无效引用 " car_engine_mileage_counters" LINE 7:ON mileage_counters.id = car_engine_mileage_counters ... ^ 提示:表" measure_instances_question_instances"有一个条目,但不能引用 从这部分查询。
枚举单个FROM
子句中的所有表格,并使用WHERE AND
代替所有INNER JOIN
但是效果正常。
我的问题是,为什么?内连接查询有什么问题?我该如何解决?这有关系吗?
答案 0 :(得分:2)
UPDATE cem
SET mileage = mileage_counters.mileage * coefficients.coefficient
FROM car_engine_mileage_counters cem
INNER JOIN car_engines
ON car_engines.id = cem.engine_id
INNER JOIN engine_templates
ON car_engines.template_id = engine_templates.id
INNER JOIN mileage_counters
ON mileage_counters.id = cem.mileage_counter_id
INNER JOIN mileage_counter_templates
ON mileage_counter.template_id = mileage_counter_templates.id
INNER JOIN coefficients
ON coefficients.mileage_counter_template_id = mileage_counter_templates.id
WHERE coefficients.engine_template_id = engine_template.id AND car_engines.id = cem.engine_id;