如何在配方表中添加自引用以允许子配方?

时间:2018-06-12 22:27:57

标签: mysql sql database

我想要一个包含成分列表的表格,但也包含子食谱列表,例如:

#recipe yellow lemon juice
Ingredient         unit cost  quantity
1.Yellow lemon       Kg    1.7  1
2.--suppose it has ingredient 2
3.--suppose it has ingredient 3

#recipe mayonnaise
Ingredient                  unit cost  quantity
1.olive oil                 Lt  $2.5    1   
2.egg                       Kg  $2.5    0.12    
3.mustard                   Kg  $1.59   0.01    
4.salt                      Kg  $.9     0.01    
5.RECIPE yellow lemon juice Lt  $1.9   1 ------------this one is a recipe 

我怎么知道蛋黄酱配方使用黄柠檬汁配方? 我有以下,但不知道如何在食谱表中允许子récipes

现在情况可能是我有3个食谱A,B,C而食谱A有几个成分,但也包括食谱BC

here is fiddle

Create table recipe
(
    recipe_id int primary key,
    recipe_name varchar(30)
); 
Create table ingredient
(
    ingredient_id int primary key,
    ingredient_name varchar(30),
    ingredient_cost float
);
Create table food_ingredient
(
    fk_recipe int not null,
    fk_ingredient int not null,
    measure float,
    unit_of_measurement varchar(30)
);
INSERT INTO ingredient VALUES 
(1,'olive oil',2.5),
(2,'egg',2.5),
(3,'mustard',1.59),
(4,'salt',0.9),
(5,'lemon ingredient 1',1.9),
(6,'lemon ingredient 2',1.9),
(7,'lemon ingredient 3',1.9);


INSERT INTO recipe VALUES
(100,'Recipe lemon juice'),
(200,'Recipe MAYONNAIse');

INSERT INTO food_ingredient VALUES 
(100,5,1.0,'Lt'),
(100,6,1.0,'Lt'),
(100,7,1.0,'Lt'),
(200,1,0.2,'Lt'),   
(200,2,0.5,'Kg'),  
(200,3,0.5,'Kg'),   
(200,4,0.2,'Kg'),
(200,5,1,'Lt');

1 个答案:

答案 0 :(得分:0)

我可能会添加一个:

def get_comment_status(row):
    if row['address'] == 'NY':
        return ['call tomorrow', 'interview scheduled']
    else:
        return ['Dont call', 'application rejected']

df[['comment', 'selection_status']] = df.apply(get_comment_status, axis=1).values.tolist()

print(df)

  address  name1  name2        comment  score      selection_status
0      NY   john  mayer  call tomorrow     90   interview scheduled
1      CA  mayer  dylan      Dont call      8  application rejected
2      NJ  dylan  mayer      Dont call     88  application rejected
3      NY    bob    bob  call tomorrow     72   interview scheduled
4      WS   mary    bob      Dont call     34  application rejected
5      OR   jake    tim      Dont call     95  application rejected
6      OR    rob    ben      Dont call     50  application rejected

我还要在我的蛋黄酱中添加一些糖和醋...... :)。