从多个相关表中获取关系数据

时间:2013-03-05 09:43:02

标签: mysql select join relational

我正在研究小型游戏的数据库结构,这款游戏使用“套装”作为盔甲套装。目前我有4个表:“kit”,“kit_armor”,“kit_item”和“kit_item_enchantment”。 “kit”表保存套件的ID及其名称,“kit_armor”表保存套件的装甲,“kit_item”表保存“kit_armor”列可指向的项目和“kit_item_enchantment”表持有特定物品的附加结界数据。

现在我一直在尝试查询以及我们搜索堆栈溢出和重置互联网以编写查询,这将从数据库中获取所有工具包信息,但遗憾的是没有任何结果。

这是表结构:

mysql> describe kit;
+----------+-----------------+------+-----+---------+----------------+
| Field    | Type            | Null | Key | Default | Extra          |
+----------+-----------------+------+-----+---------+----------------+
| kit_id   | int(4) unsigned | NO   | PRI | NULL    | auto_increment |
| kit_name | varchar(32)     | NO   | UNI | NULL    |                |
+----------+-----------------+------+-----+---------+----------------+
2 rows in set (0.01 sec)

mysql> describe kit_armor;
+------------+-----------------+------+-----+---------+-------+
| Field      | Type            | Null | Key | Default | Extra |
+------------+-----------------+------+-----+---------+-------+
| kit_id     | int(4) unsigned | NO   | PRI | NULL    |       |
| helmet     | int(4) unsigned | YES  | MUL | NULL    |       |
| chestplate | int(4) unsigned | YES  | MUL | NULL    |       |
| leggings   | int(4) unsigned | YES  | MUL | NULL    |       |
| boots      | int(4) unsigned | YES  | MUL | NULL    |       |
+------------+-----------------+------+-----+---------+-------+
5 rows in set (0.01 sec)

mysql> describe kit_item;
+------------+-----------------+------+-----+---------+----------------+
| Field      | Type            | Null | Key | Default | Extra          |
+------------+-----------------+------+-----+---------+----------------+
| id         | int(4) unsigned | NO   | PRI | NULL    | auto_increment |
| item_id    | int(4) unsigned | NO   |     | NULL    |                |
| stack_size | int(4) unsigned | NO   |     | NULL    |                |
+------------+-----------------+------+-----+---------+----------------+
3 rows in set (0.01 sec)

mysql> describe kit_item_enchantment;
+----------------+-----------------+------+-----+---------+-------+
| Field          | Type            | Null | Key | Default | Extra |
+----------------+-----------------+------+-----+---------+-------+
| item_id        | int(4) unsigned | NO   | PRI | NULL    |       |
| enchantment_id | int(4) unsigned | NO   |     | NULL    |       |
| tier           | int(4) unsigned | NO   |     | NULL    |       |
+----------------+-----------------+------+-----+---------+-------+
3 rows in set (0.01 sec)

参考专栏:

+---------------------------+--------------------+-------------------------------+-----------------
| TABLE_NAME                | COLUMN_NAME        | REFERENCED_TABLE_NAME | REFERENCED_COLUMN_NAME |
+---------------------------+--------------------+-------------------------------+-----------------
| kit_armor                 | kit_id             | kit                   | kit_id                 |
| kit_armor                 | helmet             | kit_item              | id                     |
| kit_armor                 | chestplate         | kit_item              | id                     |
| kit_armor                 | leggings           | kit_item              | id                     |
| kit_armor                 | boots              | kit_item              | id                     |
| kit_item_enchantment      | item_id            | kit_item              | id                     |

我的问题是获取所有工具包信息的最佳方法是什么,以及是否可以在单个查询中进行此操作(JOINs?)或者我是否必须将其拆分为多个查询?

这对我很有帮助,自昨天以来我一直在努力解决这个问题,同时我已经对从数据库中选择关系数据的最佳方法进行了大量的研究,而且我只遇到了非常复杂的指南和查询令人难以置信的混淆。

提前致谢,

纳斯

0 个答案:

没有答案