gpt4 book ai didi

php - 如何将多个表链接到一个 ID

转载 作者:搜寻专家 更新时间:2023-10-31 22:07:14 24 4
gpt4 key购买 nike

我正在尝试创建一个数据库(使用 PHP 和 MYSQL,以及 XAMPP)来存储有关各种快餐店及其分店的信息,以及他们分店提供的菜单。我在我的数据库中创建了一些表有关快餐店、分店、菜单的类别和子类别以及他们在表格中提供的项目的详细信息。表格的摘录/示例如下所示。

Restaurants
+-------------+----------------+
| RestaurantID| RestaurantName |
+-------------+----------------+
| 1 | PizzaHut |
| 2 | BurgerKing |
| 3 | KFC |
| 4 | SubWay |
+-------------+----------------+

Branches
+---------------+----------------+
| RestaurantName| BranchAddress |
+---------------+----------------+
| KFC | XXX-1234 |
| KFC | AAA-1234 |
| KFC | YYY-1234 |
| BurgerKing | DDD-1234 |
+---------------+----------------+

Categories
+---------------+----------------+
| BranchAddress | Categories |
+---------------+----------------+
| XXX-1234 | Fries |
| AAA-1234 | Burgers |
| YYY-1234 | Drinks |
| DDD-1234 | Burgers |
+---------------+----------------+

SubCategories
+---------------+----------------+
| Categories | SubCategories |
+---------------+----------------+
| Fries | Cheese |
| Burgers | Chicken |
| Drinks | Carbonated |
| Burgers | Beef |
+---------------+----------------+

Items
+---------------+----------------+
| SubCategories | Items |
+---------------+----------------+
| Cheese | CheeseFries |
| Chicken | Zinger |
| Carbonated | Pepsi |
| Beef | Whopper |
+---------------+----------------+

如上所示,每个餐厅可以有多个分店,每个分店可以有多个类别,每个类别可以有几个子类别,子类别可以有几个项目。分店可以有相同的类别和/或子-类别,但其中有不同的项目。

我认为集中式类别、子类别和项目表在这种情况下不起作用,因为每家餐厅的每个分店都是独立的个体实体,这需要自己的一组类别、子类别和项目表。如果是这样,我该如何着手创建表以及设置必要的外键?

Categories
+----------------+
| Categories |
+----------------+
| Drinks |
| Burgers |
| Fries |
| SetMeals |
+----------------+

SubCategories
+----------------+----------------+
| Category | SubCategories |
+---------------+-----------------+
| Drinks | Carbonated |
| Drinks | Carbonated |
| Drinks | Carbonated |
| Drinks | Non-Carbonated |
| Burgers | Beef |
| Burgers | Beef |
| Burgers | Chicken |
+----------------+----------------+

SubCategories
+----------------+----------------+-------------+
| Category | SubCategories | Items |
+---------------+-----------------+-------------+
| Drinks | Carbonated | Pepsi |
| Drinks | Carbonated | Root Beer |
| Drinks | Carbonated | Cola |
| Drinks | Non-Carbonated | Orange Juice |
| Burgers | Beef | Whopper |
| Burgers | Beef | Cheese Burger|
| Burgers | Chicken | McChicken |
+----------------+----------------+--------------+

当然,我可能是错的,集中表可能适用于这种情况。如果是这样,我能否提供一些关于如何继续/或着手设置表格的示例和/或说明?

我想到了以下解决方案:

Restaurants
+-------------+----------------+
| RestaurantID| RestaurantName |
+-------------+----------------+
| 1 | PizzaHut |
| 2 | BurgerKing |
| 3 | KFC |
| 4 | SubWay |
+-------------+----------------+

Branches
+---------------+----------------+
| RestaurantName| BranchAddress |
+---------------+----------------+
| KFC | XXX-1234 |
| KFC | AAA-1234 |
| KFC | YYY-1234 |
| BurgerKing | DDD-1234 |
+-------------+------------------+

Categories
+----------------+----------------+----------------+
| RestaurantName | BranchAddress | Category |
+----------------+-----------------+----------------+
| KFC | XXX-1234 | Fries |
| KFC | AAA-1234 | Burgers |
| KFC | YYY-1234 | Drinks |
| BurgerKing | DDD-1234 | Burgers |
+----------------+----------------------------------+

SubCategories
+----------------+----------------+----------------+----------------+
| RestaurantName | BranchAddress | Category | SubCategories |
+----------------+-----------------+---------------+----------------+
| KFC | XXX-1234 | Fries | Cheese |
| KFC | AAA-1234 | Burgers | Chicken |
| KFC | YYY-1234 | Drinks | Carbonated |
| BurgerKing | DDD-1234 | Burgers | Beef |
+----------------+---------------------------------+----------------+

Items
+----------------+----------------+----------------+----------------+-------------+
| RestaurantName | BranchAddress | Category | SubCategories | Items |
+----------------+-----------------+---------------+----------------+-------------+
| KFC | XXX-1234 | Fries | Cheese |CheeseFries |
| KFC | AAA-1234 | Burgers | Chicken |Zinger |
| KFC | YYY-1234 | Drinks | Carbonated |Pepsi |
| BurgerKing | DDD-1234 | Burgers | Beef |Whopper |
+----------------+---------------------------------+----------------+-------------+

虽然上面的表格为每家餐厅及其分店提供了各自的类别、子类别、项目,但它首先违背了将它们分开的全部目的,而且非常愚蠢。

编辑:我试图对上面的表进行非规范化,以便我可以使用

查询它
$query="SELECT * FROM Items WHERE Restaurantname='BurgerKing' AND 
BranchAddress='XXX-1234' AND Category='Fries';
$result=mysqli_query($cxn,$query) or die("Error");
while($row=mysqli_fetch_assoc)
{
extract($row);
echo "$Item";
}

注意:我计划在未来扩展数据库以包括非快餐店甚至非 F&B 相关商店,因此各种表格(类别/子类别和项目)必须能够包括非餐饮相关商店的类别(例如电子产品/书籍等)

对于非常长的问题,我深表歉意,如果我能得到任何建议和帮助,我将不胜感激。

谢谢!

最佳答案

也不确定您为什么要进行非规范化。您可以将与分支相关的列从类别和子类别中取出,将类别名称从子类别中取出,等等。将它们保留在其中会复制大量数据,而这不是传统上使用关系数据库的方式!您还应该通过 ID 而不是名称来关联这些表,因为加入时的比较速度更快。

您可以考虑使用多对多链接表将项目链接到分支。这样,您就可以满足将类别/子类别的特定项目与分支相关联而无需关联整个类别/子类别的要求。

CREATE TABLE item_link (
id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
branch_id INT(11) NOT NULL,
item_id INT(11) NOT NULL,
KEY composite (`branch_id`, `item_id`)
);

查看此 fiddle我为您详细介绍了我对您的模式提出的更改建议,并举例说明了您将如何使用这个新表来显示分店的菜单/库存。

关于php - 如何将多个表链接到一个 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16751931/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com