访问'加入不受支持'移动LEFT加入

时间:2018-03-15 20:00:57

标签: sql ms-access join left-join

我在这里做了一些研究,我理解在Access嵌套连接中会导致问题。

我认为这是第一个例子中的问题。

SELECT 
Recipe_Classes.RecipeClassDescription,
Recipes.RecipeTitle,
Recipes.Preparation,
Ingredients.IngredientName,
Recipe_Ingredients.RecipeSeqNo,
Recipe_Ingredients.Amount,
Measurements.MeasurementDescription
FROM (((
Recipe_Classes
LEFT JOIN Recipes
ON Recipe_Classes.RecipeClassID = Recipes.RecipeClassID)
INNER JOIN Recipe_Ingredients
ON Recipes.RecipeID = Recipe_Ingredients.RecipeID)
INNER JOIN Ingredients
ON Ingredients.IngredientID = Recipe_Ingredients.IngredientID)
INNER JOIN Measurements
ON Measurements.MeasureAmountID = Recipe_Ingredients.MeasureAmountID
ORDER BY RecipeTitle, RecipeSeqNo;

我尝试删除嵌套并在此示例中创建了一个右连接

 SELECT 
    Recipe_Classes.RecipeClassDescription,
    Recipes.RecipeTitle,
    Recipes.Preparation,
    Ingredients.IngredientName,
    Recipe_Ingredients.RecipeSeqNo,
    Recipe_Ingredients.Amount,
    Measurements.MeasurementDescription
    FROM (((
    Ingredients
    INNER JOIN Recipe_Ingredients
    ON Ingredeints.IngredientID = Recipe_Ingredients.IngredientID)
    INNER JOIN Measurements
    ON Measurements.MeasureAmountID = Recipe_Ingredients.MeasureAmountID)
    INNER JOIN Recipes
    ON Recipes.RecipeID = Recipe_Ingredients.RecipeID)
    RIGHT JOIN Recipe_Classes
    ON Recipe_Classes.RecipeClassID = Recipes.RecipeClassID
    ORDER BY RecipeTitle, RecipeSeqNo;

有人能指出我正确的方向吗?

2 个答案:

答案 0 :(得分:1)

问题可能是不需要的left join。试试这个from条款:

SELECT Recipe_Classes.RecipeClassDescription,
       Recipes.RecipeTitle,
       Recipes.Preparation,
       Ingredients.IngredientName,
       Recipe_Ingredients.RecipeSeqNo,
       Recipe_Ingredients.Amount,
       Measurements.MeasurementDescription
FROM (((Recipes LEFT JOIN
        Recipe_Classes
        ON Recipe_Classes.RecipeClassID = Recipes.RecipeClassID
       ) LEFT JOIN
       Recipe_Ingredients
       ON Recipes.RecipeID = Recipe_Ingredients.RecipeID
      ) LEFT JOIN
      Ingredients
      ON Ingredients.IngredientID = Recipe_Ingredients.IngredientID
     ) LEFT JOIN Measurements
     ON Measurements.MeasureAmountID = Recipe_Ingredients.MeasureAmountID
ORDER BY RecipeTitle, RecipeSeqNo;

LEFT JOIN开始,通常所有剩余的联接也应该是LEFT JOIN s。

答案 1 :(得分:0)

由于您的主要焦点表是 Recipe_Ingredients ,请考虑从FROM中的此表开始,然后JOIN其他父表,甚至嵌套食谱< / em>和 Recipe_Classes 对:

SELECT 
    c.RecipeClassDescription,
    r.RecipeTitle,
    r.Preparation,
    i.IngredientName,
    ri.RecipeSeqNo,
    ri.Amount,
    m.MeasurementDescription
FROM 
  ((Recipe_Ingredients ri    
    INNER JOIN (Recipes r 
                RIGHT JOIN Recipe_Classes c
                      ON c.RecipeClassID = r.RecipeClassID)
          ON r.RecipeID = ri.RecipeID)
    INNER JOIN Ingredients i
          ON i.IngredientID = ri.IngredientID)
    INNER JOIN Measurements m
          ON m.MeasureAmountID = ri.MeasureAmountID

ORDER BY r.RecipeTitle, ri.RecipeSeqNo;

当然,如果没有数据,这是未经测试的。由于Access&#39; JOIN要求如parnetheses,通常建议新用户使用MS Access构建复杂查询。然后,GUI Query Design会根据需要修改生成的SQL。