【问题标题】:How to record sequential collections of records in MySQL如何在 MySQL 中记录连续的记录集合
【发布时间】:2012-03-08 07:51:40
【问题描述】:

假设我有一张桌子,上面有某种类型的记录,比如"Fold the melted chocolate into the egg whites" 之类的烹饪说明。该表包含一个唯一的 ID 字段和字符串。

我想为食谱(每个都有唯一的 ID 和名称)构建另一个表,每个表都是一系列顺序指令(一些指令将用于多个/多个食谱)。

构建配方表以将配方的唯一 ID 映射到顺序系列指令(哪些 ID 不是顺序的)的最佳方法是什么?

【问题讨论】:

    标签: mysql sql


    【解决方案1】:

    尝试这样的标准化设计:

    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
    

    要获取特定食谱的说明列表,您可以使用以下查询:

    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
    

    【讨论】:

    • 不错。通过将每条指令映射到 1 个 recipe_ID,我什至可以让食谱递归......
    猜你喜欢
    • 1970-01-01
    • 2014-05-11
    • 2014-06-05
    • 2017-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-18
    • 1970-01-01
    相关资源
    最近更新 更多