MySQL 新手寻找多对多关系查询
一些背景我有一组数据,代表天际的炼金术成分及其效果。如果您对此不熟悉,您可以将 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,但是你不需要在成分表上添加effect1到effect4。
是的。获取所需数据的唯一方法是将三个表连接在一起。如果没有外键(或更具体地说,没有适当的索引),查询可能无法很好地执行。当然,总体上行数确实很少,但在这种情况下使用外键是一个很好的做法。
我认为您追求的是这样的结果:
如果您需要查看多种成分的效果,您可以调整 WHERE 子句,如下所示:
您可能不想要重复的效果,因此您可以执行 SELECT DISTINCT 来消除这些效果。
《天际》中的药水效果可以叠加吗?如果它们可以堆叠,那么您可以使用 COUNT 执行 GROUP BY 查询来获取每个效果的堆叠值:
此查询将列出 6 个效果,值为 1,“恢复耐力”的值为 2。不确定《天际》药剂是否以这种方式起作用,但这只是一个额外的想法。
Yes, but then you don't need to have effect1 through effect4 on the ingredient table.
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.
I think you're after something like this:
If you need to see effects for multiple ingredients, you could adjust your WHERE clause, like this:
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:
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.