LEFT JOIN 3 列获取用户名

发布于 2024-11-15 03:16:11 字数 1529 浏览 2 评论 0原文

我需要加入三列,它们来自 3 个不同的表,

贡献表:

+-----------+---------------------+
| record_id | contributor_user_id |
+-----------+---------------------+
|         1 |                   2 |
+-----------+---------------------+
|         1 |                   5 |
+-----------+---------------------+

成员表:

+--------------+---------+
| username     | user_id |
+--------------+---------+
| Test         |       1 |
+--------------+---------+
| Test2        |       5 |
+--------------+---------+
| Test3        |       6 |
+--------------+---------+

记录表:

+---------+-----------+
| user_id | record_id |
+---------+-----------+
|      28 |         1 |
+---------+-----------+

对于我需要的内容return 是用于显示记录所有者的usernameuser_id。此外,还显示 usernameuser_id,但这可以是多个(超过 1 个用户)。我试过这个:

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

I have three columns I need to join which comes from 3 different tables,

Contributions table:

+-----------+---------------------+
| record_id | contributor_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

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

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

发布评论

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

评论(2

萌梦深 2024-11-22 03:16:11

尝试将贡献用户的联接嵌套在贡献的左联接内。

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

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
从﹋此江山别 2024-11-22 03:16:11
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
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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文