MySQL 新手寻找多对多关系查询

发布于 2025-01-01 10:25:54 字数 559 浏览 2 评论 0原文

一些背景我有一组数据,代表天际的炼金术成分及其效果。如果您对此不熟悉,您可以将 2-4 种成分混合起来制成药水。每种成分都有4种功效。如果成分之间的任何效果相同,就会制成这种类型的药水。我已将其确定为多对多关系,并按如下方式设置表格:

成分:ing_id(键),ing_name,(其他补充信息)

效果:eff_id(键),eff_name

ing_eff_xref:eff_id,ing_id

I想要输入 2 种或更多可用成分并返回可能的组合,但不知道效果是什么。我的 sql 经验几乎仅限于 phpmyadmin 和简单的选择查询。我想我的问题是:这是为这种类型的关系构建表的正确方法吗?如果我不打算更新表,我是否需要设置外键?是否有一个查询可以接受一组ing_names 并仅返回相交的 eff_names?

如果有人感兴趣,这里是数据库的 mysqldump: http://dl.dropbox.com /u/59699040/alchemy_db.sql

Some background I have a set of data that represents the alchemy ingredients and their effects from Skyrim. If you're unfamiliar with this you can combine 2-4 ingredients to make a potion. Each ingredient has 4 effects. If any effects between ingredients are the same it will make that type of potion. I've identified this as a many-to-many relationship and I set up my tables like so:

ingredients: ing_id (key), ing_name, (other supplemental info)

effects: eff_id (key), eff_name

ing_eff_xref: eff_id, ing_id

I would like to input 2 or more available ingredients and return possible combinations without knowing what the effects are. My sql experience is pretty much limited to phpmyadmin and simple select queries. I guess my questions are: is this the right way to structure the tables for this type of relationship, do I need to set foreign keys if I don't plan on updating the tables, and is there a query that can take a set of ing_names and return only eff_names that intersect?

Here is the mysqldump of the db if anyone is interested: http://dl.dropbox.com/u/59699040/alchemy_db.sql

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

若言繁花未落 2025-01-08 10:25:54

这是为此类关系构建表格的正确方法吗?

是的,但是你不需要在成分表上添加effect1到effect4。

如果我不打算更新表,是否需要设置外键?

是的。获取所需数据的唯一方法是将三个表连接在一起。如果没有外键(或更具体地说,没有适当的索引),查询可能无法很好地执行。当然,总体上行数确实很少,但在这种情况下使用外键是一个很好的做法。

是否有一个查询可以接受一组 ing_names 并仅返回
eff_names 相交吗?

我认为您追求的是这样的结果:

SELECT e.eff_name
FROM ingredients i
INNER JOIN ing_eff_xref ie ON ie.ing_id = i.ing_id
INNER JOIN effects e ON e.eff_id = ie.eff_id
WHERE i.ing_name = 'Abecean Longfin ';

如果您需要查看多种成分的效果,您可以调整 WHERE 子句,如下所示:

WHERE i.ing_name IN ('Abecean Longfin ','Eye of Sabre Cat ','Bear Claws ');

您可能不想要重复的效果,因此您可以执行 SELECT DISTINCT 来消除这些效果。

《天际》中的药水效果可以叠加吗?如果它们可以堆叠,那么您可以使用 COUNT 执行 GROUP BY 查询来获取每个效果的堆叠值:

SELECT e.eff_name, count(*) as value
FROM ingredients i
INNER JOIN ing_eff_xref ie ON ie.ing_id = i.ing_id
INNER JOIN effects e ON e.eff_id = ie.eff_id
WHERE i.ing_name IN ('Eye of Sabre Cat ','Bear Claws ')
GROUP BY e.eff_name;

此查询将列出 6 个效果,值为 1,“恢复耐力”的值为 2。不确定《天际》药剂是否以这种方式起作用,但这只是一个额外的想法。

is this the right way to structure the tables for this type of relationship?

Yes, but then you don't need to have effect1 through effect4 on the ingredient table.

do I need to set foreign keys if I don't plan on updating the tables?

Yes. The only way for you to get the data that you're after is by JOINing three tables together. Without foreign keys (or more specifically, appropriate indexes), that may not perform well on queries. Of course you do have a small number of rows overall, but using foreign keys is a good practice to follow in this type of scenario.

is there a query that can take a set of ing_names and return only
eff_names that intersect?

I think you're after something like this:

SELECT e.eff_name
FROM ingredients i
INNER JOIN ing_eff_xref ie ON ie.ing_id = i.ing_id
INNER JOIN effects e ON e.eff_id = ie.eff_id
WHERE i.ing_name = 'Abecean Longfin ';

If you need to see effects for multiple ingredients, you could adjust your WHERE clause, like this:

WHERE i.ing_name IN ('Abecean Longfin ','Eye of Sabre Cat ','Bear Claws ');

You'll probably not want duplicate effects, so you could do a SELECT DISTINCT to eliminate those.

Can potion effects stack in Skyrim? If they in can stack, then you can do a GROUP BY query with a COUNT to get the stacked value of each effect:

SELECT e.eff_name, count(*) as value
FROM ingredients i
INNER JOIN ing_eff_xref ie ON ie.ing_id = i.ing_id
INNER JOIN effects e ON e.eff_id = ie.eff_id
WHERE i.ing_name IN ('Eye of Sabre Cat ','Bear Claws ')
GROUP BY e.eff_name;

This query will list 6 effects with a value of 1, and "Restore Stamina" will have a value of 2. Not sure if Skyrim potions work this way or not, but it was just an extra thought.

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