Mysql, storing multiple value in single column from another table

前端 未结 3 476
攒了一身酷
攒了一身酷 2020-12-15 10:23


Bear with me, im really bad at explaining thing and i dont even know an appropriate title for this problem
Ok guys i have this problem
I already have one table

3条回答
  •  鱼传尺愫
    2020-12-15 11:19

    This is called a many-to-many relationship between meals and combo. A meal can be listed in multiple combos and a combos can contain multiple meals. You will need a link table (instead of the combo.meal_id field) that contains all possible meal-combo pairs.

    In the end, you will have three tables:

    1. meal (meal_id, serving, name)
    2. combo (combo_id, serving, name)
    3. meal_combo (autoid, meal_id, combo_id)

    meal_combo.autoid is not strictly needed, it's just a general recommendation.

    To list a combo with all it's meals in it:

    SELECT meal.id, meal.name FROM comboINNER JOIN meal_combo ON meal_combo.combo_id = combo.id INNER JOIN meal ON meal.id = meal_combo.meal_id WHERE combo.id = 132

    Google for 'many-to-many relationship' or 'database link table' for details.

提交回复
热议问题