Mysql,在另一个表的单列中存储多个值
请耐心听我说,我真的不擅长解释事情,我什至不知道这个问题的合适标题
好的,我有这个问题
我已经有一个名为 meal
的桌子
+------+--------+-----------+---------+
| id | name | serving | price |
+------+--------+-----------+---------+
| 1 | soup1 | 2 person | 12.50 |
+------+--------+-----------+---------+
| 2 | soup2 | 2 person | 15.50 |
+------+--------+-----------+---------+
| 3 | soup3 | 2 person | 23.00 |
+------+--------+-----------+---------+
| 4 | drink1 | 2 person | 4.50 |
+------+--------+-----------+---------+
| 5 | drink2 | 2 person | 3.50 |
+------+--------+-----------+---------+
| 6 | drink3 | 2 person | 5.50 |
+------+--------+-----------+---------+
| 7 | frui1 | 2 person | 3.00 |
+------+--------+-----------+---------+
| 8 | fruit2 | 2 person | 3.50 |
+------+--------+-----------+---------+
| 9 | fruit3 | 2 person | 4.50 |
+------+--------+-----------+---------+
了,现在我想允许管理员从此 meal
表创建组合餐
这意味着,组合餐可以有无限数量的餐
目前我困惑如何存储/链接组合餐到餐 我不想存储类似下面的内容
+------+--------------+-----------+-----------+
| id | combo_name | serving | meal_id |
+------+--------------+-----------+-----------+
| 1 | combo1 | 2 person | 1,4,7,9 |
+------+--------------+-----------+-----------+
| 2 | combo2 | 2 person | 2,5,8 |
+------+--------------+-----------+-----------+
| 4 | combo3 | 2 person | 3,5,6,9 |
+------+--------------+-----------+-----------+
查看 meal_id
列,我认为这不是存储数据的好方法
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 name meal
+------+--------+-----------+---------+
| id | name | serving | price |
+------+--------+-----------+---------+
| 1 | soup1 | 2 person | 12.50 |
+------+--------+-----------+---------+
| 2 | soup2 | 2 person | 15.50 |
+------+--------+-----------+---------+
| 3 | soup3 | 2 person | 23.00 |
+------+--------+-----------+---------+
| 4 | drink1 | 2 person | 4.50 |
+------+--------+-----------+---------+
| 5 | drink2 | 2 person | 3.50 |
+------+--------+-----------+---------+
| 6 | drink3 | 2 person | 5.50 |
+------+--------+-----------+---------+
| 7 | frui1 | 2 person | 3.00 |
+------+--------+-----------+---------+
| 8 | fruit2 | 2 person | 3.50 |
+------+--------+-----------+---------+
| 9 | fruit3 | 2 person | 4.50 |
+------+--------+-----------+---------+
Ok now i want to allow admin to create a combo meal from this meal
table
So that mean, a combo meal can have unlimited number amout of meal
Currently im puzzle how to store/link combo meal to the meal
I donw want to store something lke below
+------+--------------+-----------+-----------+
| id | combo_name | serving | meal_id |
+------+--------------+-----------+-----------+
| 1 | combo1 | 2 person | 1,4,7,9 |
+------+--------------+-----------+-----------+
| 2 | combo2 | 2 person | 2,5,8 |
+------+--------------+-----------+-----------+
| 4 | combo3 | 2 person | 3,5,6,9 |
+------+--------------+-----------+-----------+
Look at the meal_id
column, i dont think that is a good way to store a data
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
创建多对多链接表:
要选择给定组合的所有餐点:
Create a many-to-many link table:
To select all meals for a given combo:
不,这绝对不是存储数据的好方法。使用
combo_header
表和combo_details
表会更好。combo_header
将类似于:然后,
combo_details
将类似于:顺便说一下,通过在单个列中使用多个值,您违反了关系数据库的第一范式。
我建议的方式将让您回答诸如获取组合1的所有餐点名称之类的查询,非常容易解决。
No. It's definitely not a good way to store data. You will be better with a
combo_header
table and acombo_details
table.combo_header
will be something like:And then,
combo_details
will be something like:By the way, by using multiple values in a single column you are violating first normal form of relational databases.
The way I'm proposing will let you answer queries like get all name of the meals of combo1 very easy to resolve.
这称为膳食和组合之间的多对多关系。一顿饭可以在多个组合中列出,并且一个组合可以包含多顿饭。您将需要一个包含所有可能的膳食组合对的链接表(而不是combo.meal_id 字段)。
最后,您将拥有三个表:
meal_combo.autoid不是严格需要的,它只是一般建议。
要列出包含所有餐点的组合:
SELECTmeal.id,meal.nameFROMcomboINNERJOINmeal_comboONmeal_combo.combo_id=combo.idINNERJOINmealONmeal.id=meal_combo.meal_idWHEREcombo.id= 132
Google 搜索“多对多关系”或“数据库链接表”了解详细信息。
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:
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.