如何在INNER JOIN语句中使用括号

时间:2017-10-05 00:11:40

标签: mysql sql join

我似乎无法弄清楚在innerjoin语句中正确使用括号。例如,当在此语句中执行总共4个INNERJOINS时,为什么需要在FROM子句之后放置3个括号。

SELECT Recipe_Classes.RecipeClassDescription,Recipes.RecipeTitle,Recipes.Preparation, Ingredients.IngredientName, Recipe_Ingredients.RecipeSeqNo,   Recipe_Ingredients.Amount, Measurements.MeasurementDescription 
FROM (((Recipe_Classes 
INNER 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

1 个答案:

答案 0 :(得分:3)

不需要括号。但建议使用表别名!

SELECT rc.RecipeClassDescription, Recipes.RecipeTitle, r.Preparation, 
       i.IngredientName, ri.RecipeSeqNo, ri.Amount, m.MeasurementDescription 
FROM Recipe_Classes rc INNER JOIN
     Recipes r
     ON rc.RecipeClassID = r.RecipeClassID INNER JOIN
     Recipe_Ingredients ri
     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 RecipeTitle, RecipeSeqNo;

虽然括号在理论上是有用的(在某些情况下有外连接),但我发现它们在实践中并不需要它们。唯一的例外是MS Acccess,它需要多个连接的括号。