How to record sequential collections of records in MySQL

前端 未结 1 1191
梦毁少年i
梦毁少年i 2021-01-22 20:44

Suppose I have a table that holds some type of record, say cooking instructions like \"Fold the melted chocolate into the egg whites\". The table contains a unique

相关标签:
1条回答
  • 2021-01-22 20:59

    Try a normalized design like this:

    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 the 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
    
    0 讨论(0)
提交回复
热议问题