0

Looking for a solution to build a database for recipe generation. A number of ingredients are used to create recipe and further a recipe can also contain a recipe plus can also have some ingredients.

For example: recipe R1 needs three products i.e I1, I2, I3.

But recipe R2 needs recipe R1 and in addition to it some other ingredients, maybe I4, I5, I6

So

R1 = I1 + I2 + I3 

whereas

R2 = R1 + I4 + I5 + I6

To accomplish this, I have an Ingredient table like below:

Id name
--------    
1  I1
2  I2
3  I3
4  I4
5  I5
6  I6

Now I need maping where I can map Recipe with ingredients like

Recipe table

Id Name
------------   
1  Recipe1
2  Recipe2

Recipe FormulationTable

Id   RecipeId   IngredientId
-----------------------------
1    1           1
2    1           2
3    1           3
4    2           4
5    2           5
6    2           6
7    2           

but here I can not add any ingredient as recipe 2 needs 4,5,6 and recipe 2

How can I map these tables so that I may fetch any kind of information at my dashboard

Like->

  • Recipe 1 has three ingredients
  • but Recipe 2 has other three ingredients and also it contains receipe1
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Sweetie
  • 1,298
  • 6
  • 24
  • 48
  • **TYPO** alert - it's **recipe** - not "receipe" ... – marc_s Feb 05 '20 at 06:58
  • Does this answer your question? [How to create a MySQL hierarchical recursive query](https://stackoverflow.com/questions/20215744/how-to-create-a-mysql-hierarchical-recursive-query) – philipxy Feb 05 '20 at 09:43
  • This is a faq. Before considering posting please always google any error message or many clear, concise & precise phrasings of your question/problem/goal, with & without your particular strings/names & site:stackoverflow.com & tags, & read many answers. If you post a question, use one phrasing as title. See [ask] & the voting arrow mouseover texts. – philipxy Feb 05 '20 at 09:44

1 Answers1

1

I would suggest you to create another table Recipe_Sub to handle the sub ingredients. So that table RecipeFormulation only store Recipe-Ingredients mapping.

table Recipe_Sub

table Recipe_Sub

table RecipeFormulation

enter image description here

then create a view to join all data.

CREATE VIEW [dbo].[vRecipeFormulationFull] AS
    select rs.RecipeId, rf.IngredientId
    from Recipe_Sub rs
    inner join RecipeFormulation rf on rs.RecipeId_Main = rf.RecipeId

    union all

    select RecipeId, IngredientId
    from RecipeFormulation
GO

result:

select *
from vRecipeFormulationFull vrff
inner join Recipe r on vrff.RecipeId = r.Id
inner join Ingredient i on vrff.IngredientId = i.Id
order by r.Name, i.Name

Result

Harlo
  • 507
  • 2
  • 12
  • 1
    Please [use text, not images/links, for text--including tables & ERDs](https://meta.stackoverflow.com/q/285551/3404097). Paraphrase or quote from other text. Give just what you need & relate it to your problem. Use images only for what cannot be expressed as text or to augment text. Images cannot be searched for or cut & pasted. Include a legend/key & explanation with an image. – philipxy Feb 05 '20 at 09:44