Hi all,
Trying my hand at PHP and that of course means MySQL as well. My first project (a recipe DB for my wife) is working fine but I'm think my current method is not great.
My knowledge of SQL is pretty poor so I currently have the two below SQL statements in use. One to grab the recipe and related information and the other to grab the recipes ingredients. It is currently like this because of the ingredients many-to-many relationship to the recipe and because I'm putting the two different result sets in different tables.
1) I imagine it is bad practice to hit the database twice like this?
2) How should I go about combining these two statements?
-2a) If you could just maybe bump me in the right direction instead of just giving the answer. I'd like to learn.
Please let me know if I can provide more information.
Thanks!
Code:SELECT RecipeName, Servings, BakeTime, Instructions, ( SELECT Category FROM category WHERE category.CategoryID = recipe.CategoryID ) AS Category, ( SELECT Source FROM source WHERE source.SourceID = recipe.SourceID ) AS Source, ( SELECT Holiday FROM holiday WHERE holiday.HolidayID = recipe.HolidayID ) AS Holiday FROM recipe WHERE recipe.RecipeID = %d;Code:SELECT ingredient.Ingredient, recipeingredient.Quantity, recipeingredient.Comments, ( SELECT Measurement FROM measurement WHERE measurement.MeasurementID = recipeingredient.MeasurementID ) AS Measurement FROM ingredient JOIN recipeingredient ON ( ingredient.IngredientID = recipeingredient.IngredientID ) WHERE recipeingredient.RecipeID = %d;




Reply With Quote