简体   繁体   English

MySQL多对一或一对多

[英]Mysql many to one OR one to many

I'm a little confused and need some help on this. 我有些困惑,需要一些帮助。 I have 3 tables. 我有3张桌子。

User_Works_on User_Works_on

+---------+-------------+--+--+
| user_id | project_id  |  |  |
+---------+-------------+--+--+
|    5812 |     1938026 |  |  |
|    6390 |     1938026 |  |  |
|   32088 |     1938026 |  |  |
|   62830 |     1938026 |  |  |
|   64436 |     1938026 |  |  |
|   64441 |     1938026 |  |  |
|   77969 |     1938026 |  |  |
+---------+-------------+--+--+

Follower_works_on Follower_works_on

+-------------+---------+
| follower_id | repo_id |
+-------------+---------+
|       64441 | 1938026 |
|       64441 |  477331 |
|       64441 |  477331 |
|        6390 | 1938026 |
|        6390 | 1529732 |
|        6390 |  477331 |
|        6390 |  477331 |
+-------------+---------+

Follows_user 关注用户

+---------+-------------+
| user_id | follower_id |
+---------+-------------+
|    5812 |        6390 |
|    5812 |       10561 |
|    5812 |       37664 |
|    5812 |       51794 |
|    6390 |          58 |
|    6390 |         302 |
|    6390 |        1340 |
|    6390 |        1471 |
|    6390 |        2316 |
|    6390 |        3448 |
|   64441 |       40399 |
|   64441 |       57411 |
|   64441 |       64111 |
|   64441 |      180542 |
|   64441 |      294196 |
+---------+-------------+

The table User_works_on show the projects which a user works on. User_works_on显示用户User_works_on的项目。 Follower_works_on shows the projects which a follower works on. Follower_works_on显示Follower_works_on者从事的项目。 Follows_users shows which users are followers of others. Follows_users显示哪些用户是其他用户的关注者。 I want to link two projects if an id is a follower any user. 如果一个ID是任何用户的关注者,我想链接两个项目。 ie i want to create a link between the project the user works on and the project the follower works on supposing there is a follower relationship between them. 即我想在用户从事的项目与追随者从事的项目之间创建链接,假设它们之间存在追随者关系。

For example: 例如:

User 5812 works on project 1938026 用户5812在项目1938026上工作
Follower 6390 works on project 1529732 追随者6390在项目1529732上工作
Follower 6390 follows User 5812 关注者6390关注用户5812
Therefore there is a link between project 1938026 and 1529732. 因此,项目1938026和1529732之间存在链接。

I'm new to mysql so i'm not sure how to frame this kind of relationship. 我是mysql的新手,所以我不确定如何构建这种关系。 Any ideas would be great. 任何想法都很棒。

A result would look like this 结果看起来像这样

+---------+-------------+-------------+-------------+
| User_id | project_id1 | project_id2 | Follower_id |
+---------+-------------+-------------+-------------+
|    5812 |     1938026 |     1938026 |        6390 |
|    5812 |     1938026 |     1529732 |        6390 |
+---------+-------------+-------------+-------------+
SELECT project_id, repo_id
FROM User_Works_on AS u
JOIN Follows_user AS fu ON u.user_id = fu.user_id
JOIN Follower_Works_on AS f ON fu.follower_id = f.follower_id

DEMO 演示

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

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