I have three columns I need to join which comes from 3 different tables,
Contributions table:
+-----------+---------------------+
| record_id | contribu开发者_开发知识库tor_user_id |
+-----------+---------------------+
|         1 |                   2 |
+-----------+---------------------+
|         1 |                   5 |
+-----------+---------------------+
Members table:
+--------------+---------+
| username     | user_id |
+--------------+---------+
| Test         |       1 |
+--------------+---------+
| Test2        |       5 |
+--------------+---------+
| Test3        |       6 |
+--------------+---------+
Records table:
+---------+-----------+
| user_id | record_id |
+---------+-----------+
|      28 |         1 |
+---------+-----------+
For what I need to return is the username and user_id for displaying the record owner. Also, display the username and the user_id, but this can be multiple (more than 1+ user). I've tried this:
SELECT usr.username,
       usr.user_id,
       rec.record_id,
       contrib.record_id,
       contrib.contributor_user_id
FROM 
(
    records rec
    INNER JOIN members usr ON rec.user_id = usr.user_id
    # this returns records as NULL
    LEFT OUTER JOIN contributions contrib ON rec.record_id = contrib.record_id AND contrib.contributor_user_id = usr.user_id
    # this works, but I need the username to be displayed too
    LEFT OUTER JOIN contributions contrib ON rec.record_id = contrib.record_id
)
WHERE rec.record_id = 1
Try nesting the join for contributing users inside of the left join to contributions.
SELECT u.username, u.user_id, r.record_id, u2.username as ContributorName, u2.user_id as ContributorId
    FROM records r
        INNER JOIN members u
            ON r.user_id = u.user_id
        LEFT JOIN contributions c
            INNER JOIN members u2
                ON c.contributor_user_id = u2.user_id
            ON r.record_id = c.record_id
    WHERE r.record_id = 1
SELECT
      usr.username             AS record_owner
    , usr.user_id              AS record_owner_id
    , rec.record_id  
    , con.contributor_user_id  AS contributor_id
    , contributors.username    AS contributor_name
FROM 
    records rec
        INNER JOIN
    members usr
            ON rec.user_id = usr.user_id
        LEFT OUTER JOIN
    contributions con
           ON rec.record_id = con.record_id 
        INNER JOIN
    members contributors
            ON con.contributor_user_id = contributors.user_id
WHERE
    rec.record_id = 1
 
         
                                         
                                         
                                         
                                        ![Interactive visualization of a graph in python [closed]](https://www.devze.com/res/2023/04-10/09/92d32fe8c0d22fb96bd6f6e8b7d1f457.gif) 
                                         
                                         
                                         
                                         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论