Mysql,在另一个表的单列中存储多个值

发布于 2024-10-14 01:56:51 字数 1575 浏览 2 评论 0原文


请耐心听我说,我真的不擅长解释事情,我什至不知道这个问题的合适标题
好的,我有这个问题
我已经有一个名为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

人│生佛魔见 2024-10-21 01:56:51

创建多对多链接表:

combo_id    meal_id
1           1
1           4
1           7
1           9
2           2
2           5
2           8
3           3
3           5
3           6
3           9

要选择给定组合的所有餐点:

SELECT  m.*
FROM    combo_meal cm
JOIN    meal m
ON      m.id = cm.meal_id
WHERE   cm.combo_id = 1

Create a many-to-many link table:

combo_id    meal_id
1           1
1           4
1           7
1           9
2           2
2           5
2           8
3           3
3           5
3           6
3           9

To select all meals for a given combo:

SELECT  m.*
FROM    combo_meal cm
JOIN    meal m
ON      m.id = cm.meal_id
WHERE   cm.combo_id = 1
稀香 2024-10-21 01:56:51

不,这绝对不是存储数据的好方法。使用 combo_header 表和 combo_details 表会更好。

combo_header 将类似于:

+------+--------------+-----------+
|  id  |  combo_name  | serving   |
+------+--------------+-----------+
|  1   |   combo1     |  2 person |
+------+--------------+-----------+
|  2   |   combo2     |  2 person |
+------+--------------+-----------+
|  4   |   combo3     |  2 person |
+------+--------------+-----------+

然后,combo_details 将类似于:

+------+-----------+
|  id  |  meal_id  |
+------+-----------+
|  1   |  1        |
+------+-----------+
|  1   |  4        |
+------+-----------+
|  1   |  7        |
+------+-----------+
|  1   |  9        |
+------+-----------+
... / you get the idea!

顺便说一下,通过在单个列中使用多个值,您违反了关系数据库的第一范式。

我建议的方式将让您回答诸如获取组合1的所有餐点名称之类的查询,非常容易解决。

No. It's definitely not a good way to store data. You will be better with a combo_header table and a combo_details table.

combo_header will be something like:

+------+--------------+-----------+
|  id  |  combo_name  | serving   |
+------+--------------+-----------+
|  1   |   combo1     |  2 person |
+------+--------------+-----------+
|  2   |   combo2     |  2 person |
+------+--------------+-----------+
|  4   |   combo3     |  2 person |
+------+--------------+-----------+

And then, combo_details will be something like:

+------+-----------+
|  id  |  meal_id  |
+------+-----------+
|  1   |  1        |
+------+-----------+
|  1   |  4        |
+------+-----------+
|  1   |  7        |
+------+-----------+
|  1   |  9        |
+------+-----------+
... / you get the idea!

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.

只为守护你 2024-10-21 01:56:51

这称为膳食和组合之间的多对多关系。一顿饭可以在多个组合中列出,并且一个组合可以包含多顿饭。您将需要一个包含所有可能的膳食组合对的链接表(而不是combo.meal_id 字段)。

最后,您将拥有三个表:

  1. meal (meal_id,serving,name)
  2. combo(combo_id,serving,name)
  3. meal_combo(autoid,meal_id,combo_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:

  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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文