How to write sequential collections of records in MySQL

Suppose I have a table that contains some type of record, such as cooking instructions, such as "Fold the melted chocolate into the egg whites" . The table contains a unique ID field and row.

I want to create another table for recipes (each with a unique identifier and name), each of which will be a sequence of consecutive instructions (some instructions will be used for several / many recipes).

What is the best way to structure my recipe table to match a unique recipe identifier in a sequence of sequential instructions (which identifiers are not sequential)?

+2
source share
1 answer

Try a normalized design:

 recipe id name 1 Recipe1 2 Recipe2 recipe_instruction recipe_id instruction_id sortorder 1 5 1 1 3 2 1 4 3 2 6 1 2 7 2 2 3 3 

To get a list of instructions for a specific recipe, you can use this query:

 SELECT i.the_string FROM recipe_instruction AS ri JOIN instruction AS i ON ri.instruction_id = i.id WHERE ri.recipe_id = 1 ORDER BY ri.sortorder 
+6
source

Source: https://habr.com/ru/post/911814/


All Articles