这是我的问题的后续:
如何在MySQL中实现多对多的层次结构 https://stackoverflow.com/questions/9691366/how-to-implement-a-many-to-many-hierarchical-structure-in-mysql
和这里:
如何在MySQL中记录记录的顺序集合 https://stackoverflow.com/questions/9610942/how-to-record-sequential-collections-of-records-in-mysql.
简而言之,我想在 MySQL 中实现一个食谱表和另一个指令表。菜谱是一系列连续的指令或其他食谱。例如你可以想象Peach_preserve
食谱,和一个Peach_tart
使用的是Peach_preserve
,加上一系列其他步骤(说明)。Peach_preserve
可用于许多其他食谱。
I read Bill Karwin 撰写的关于闭包表的博客文章 http://karwin.blogspot.com/2010/03/rendering-trees-with-closure-tables.html,我认为这个解决方案最好地解决了我的挑战(我的层次结构是多对多的,步骤是连续的)。例如我会:
recipe
id name
1 Peach preserve
2 Cubed peeled peaches
3 Fresh peaches
4 Powdered sugar
5 Cook together
6 Peel and cut in chunks
7 Mix
step (or instruction)
id desc
1 Cook together
2 Buy peaches
3 Buy sugar
4 Peel and cut in chunks
5 Mix
recipe_instruction
(Ancestor) (Descendant)
recipe_id step_id depth descendant_is_instruction
3 3 0 0
3 2 1 1
4 4 0 0
4 3 1 1
6 6 0 0
6 4 1 1
2 2 0 0
2 3 1 0
2 2 2 1
2 6 1 0
2 4 2 1
(and so on...)
我不是的粉丝descendant_is_instruction
标志,但我不知道还能怎么做。我想我可以用一个替换它descendant_is_leaf
识别终端项目...
排序顺序由包含深度为 1 的所有关系的表表示:
Depth=1 table
recipe_id step_id order
3 2 1
4 3 1
6 4 1
2 3 1
2 6 2
我在这里进行简化,因为在实践中我会将成分和说明分开,但你明白了。
那么,这是结合分层数据结构和步骤顺序概念的好方法吗?我应该做些什么来改进/简化?
菜谱是一系列连续的指令或其他菜谱。
根据人们如何阅读这句话,这可能会产生歧义。
怎么样:
配方是一系列连续的指令。
指令可以是简单的(一片叶子)或复杂的(使用另一个食谱)。
这使:
Table recipe:
- column id
- column name
- column total_cost, total_preparation_time, etc
Table instruction:
- column id
- column recipe_id
- column step_order
- column description
- column child_recipe_id (can be NULL)
所以,如果桃挞使用面团和桃子蜜饯:
select * from recipe order by id;
id name
1 Dough
2 Peach preserve
3 Peach tart
select * from instruction order by recipe_id, step_order;
id recipe_id step_order description child_recipe_id
100 1 1 Get flour NULL
101 1 2 Add water NULL
102 1 3 Mix together NULL
201 2 1 Peel peaches NULL
202 2 2 Cube peaches NULL
203 2 3 Add sugar NULL
204 2 4 Cook together NULL
301 3 1 Pre heat oven NULL
302 3 2 Prepare dough 1
303 3 3 Prepare peach 2
304 3 4 Bake NULL
没有“是叶子”标志。
如果一条指令不指向子配方,即 child_recipe_id 为 NULL,则该指令是叶子。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)