我决定存储有关我在几张桌子上制作的游戏的信息,但我遇到了一个问题:我游戏中的每个项目最多可以有三种能力,但是限制很少。这在尝试连接表并显示能力名称时会导致问题。
我正在使用MySQL服务器,我创建了一个视图,它将项目的所有信息组合成一个巨大的表。问题是如果一个项目有三种不同的能力,它就不符合视图的where条件。
以下是对此问题有重要意义的表定义(表格多于此表):
CREATE TABLE items_misc (
Item_ID INT(4) NOT NULL,
Item_Hash INT(4) NOT NULL UNIQUE,
Ability_One INT(4),
Ability_Two INT(4),
Ability_Three INT(4),
element_id INT(2) NOT NULL,
PRIMARY KEY(Item_ID),
INDEX(Item_Hash),
FOREIGN KEY(Item_ID) REFERENCES items_names(IID),
FOREIGN KEY(Ability_One) REFERENCES item_abilities(AID),
FOREIGN KEY(Ability_Two) REFERENCES item_abilities(AID),
FOREIGN KEY(Ability_Three) REFERENCES item_abilities(AID),
FOREIGN KEY(element_id) REFERENCES element_types(EID)
);
CREATE TABLE item_abilities(
AID INT(4) NOT NULL,
Name VARCHAR(30) NOT NULL UNIQUE,
Description VARCHAR(99) NOT NULL,
PRIMARY KEY(AID)
);
然后是我的CREATE VIEW语句的WHERE子句:
WHERE items_names.IID = items_stats.Item_ID AND
items_names.IID = items_misc.Item_ID AND
item_types.TID = items_stats.type_id AND
element_types.EID = items_misc.element_id AND
item_abilities.AID = items_misc.Ability_One AND
item_abilities.AID = items_misc.Ability_Two AND
item_abilities.AID = items_misc.Ability_Three;
如何更改连接条件或表格以解释每个项目可能具有来自同一能力池的三种不同能力的事实(即能力顺序无关紧要)?
答案 0 :(得分:1)
如果您希望从三个异能中获得item_abilities
的值,那么您需要在item_abilities
条款中三次引用from
:
FROM items_names,
items_stats,
items_misc,
item_types,
element_types,
item_abilities item_abilities1,
item_abilities item_abilities2,
item_abilities item_abilities3
WHERE items_names.IID = items_stats.Item_ID AND
items_names.IID = items_misc.Item_ID AND
item_types.TID = items_stats.type_id AND
element_types.EID = items_misc.element_id AND
item_abilities1.AID = items_misc.Ability_One AND
item_abilities2.AID = items_misc.Ability_Two AND
item_abilities3.AID = items_misc.Ability_Three;
顺便提一下,强烈建议您使用SQL-1999连接而不是加入where
子句:
FROM items_names
JOIN items_stats ON items_names.IID = items_stats.Item_ID
JOIN items_misc ON items_names.IID = items_misc.Item_ID
JOIN item_types ON item_types.TID = items_stats.type_id
JOIN element_types ON element_types.EID = items_misc.element_id
JOIN item_abilities item_abilities1
ON item_abilities1.AID = items_misc.Ability_One
JOIN item_abilities item_abilities2
ON item_abilities2.AID = items_misc.Ability_Two
JOIN item_abilities item_abilities3
ON item_abilities3.AID = items_misc.Ability_Three;