简体   繁体   English

MySQL 中一张表的多对多关系

[英]Many-To-Many Relationship from one Table in MySQL

I have a table with projects in it and each one has a number to identify the project uniquely.我有一张表,里面有项目,每个表都有一个数字来唯一标识项目。 I want to make a many-to-many table to link projects together and this will be done by the user through a GUI.我想制作一个多对多表来将项目链接在一起,这将由用户通过 GUI 完成。 This table would have columns project_id_1 and project_id_2.此表将包含 project_id_1 和 project_id_2 列。 I wonder what the most efficient way to query the table would be if there are many projects randomly linked together I could have something like:我想知道如果有许多项目随机链接在一起,查询表的最有效方法是什么,我可以有类似的东西:

id1 id1 id2 id2
1 1 2 2
3 3 4 4
5 5 6 6
2 2 6 6
4 4 5 5

In this case all the projects are linked together.在这种情况下,所有项目都链接在一起。 But trying to query this seems impossible without looking through the whole table.但是如果不查看整个表格,试图查询这个似乎是不可能的。

Does anyone have an idea how this could be done better?有谁知道如何做得更好?

Thanks谢谢

So in the end I didn't find any MySQL black magic.所以最后我没有找到任何MySQL黑魔法。 I just used java and multiple queries.我刚刚使用了 java 和多个查询。 It would have been nice to find a more elegant solution though.不过,找到一个更优雅的解决方案会很好。 I can't be the only one faced with such a case.我不可能是唯一一个面临这种情况的人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM