简体   繁体   中英

Multiple joins on same table

I'm trying to achieve a query which seems simple but I can't make it work correctly. Here's my database tables structures:

members
    -> id
    -> last_name
    -> first_name
activities
    -> id
registrations
    -> id
    -> member_id
tandems
    -> id
    -> activitie_id
    -> registration_member_one
    -> registration_member_two

Here's what i want to achieve:

Mutliple members can register to an activity. Then, i group the registrations by tandems. I want a view with all the tandems listed and there's my problem. When I try a query, it gives me multiple rows, duplicated many times.

Below, an example of the table I want to have:

tandems.id | activities.id | registration_member_one.members.last_name | registration_member_two.members.last_name
1          | 3             | John Doe                                  | Jane Doe

Here's the query I'm working on:

SELECT
tandems.*,
memberOne.id, memberOne.last_name, memberOne.first_name,
memberTwo.id, memberTwo.last_name, memberTwo.first_name,
memberOne_registration.member_id as memberOne,
memberTwo_registration.member_id as memberTwo

FROM tandems

JOIN registrations as memberOne_registration
ON memberOne_registration.member_id = tandems.registration_member_one
JOIN members as memberOne ON memberOne.id = memberOne_registration.member_id

JOIN registrations as memberTwo_registration
ON memberTwo_registration.member_id = tandems.registration_member_two
JOIN members as memberTwo ON memberTwo.id = memberTwo_registration.member_id

WHERE activitie_id = 3;

Any help appreciated!

The error is caused by joining wrong column ( member_id ) of registrations table with tandems table, instead column registrations.id should be used.

SELECT
tandems.*,
memberOne.id, memberOne.last_name, memberOne.first_name,
memberTwo.id, memberTwo.last_name, memberTwo.first_name,
memberOne_registration.id as memberOne,
memberTwo_registration.id as memberTwo

FROM tandems
JOIN registrations as memberOne_registration ON memberOne_registration.id = tandems.registration_member_one
JOIN members as memberOne ON memberOne.id = memberOne_registration.member_id
JOIN registrations as memberTwo_registration ON memberTwo_registration.id = tandems.registration_member_two
JOIN members as memberTwo ON memberTwo.id = memberTwo_registration.member_id

WHERE activitie_id = 3;

Although other query is virtually the same, I hate working with unnecessarily long alias names so worked with "r1" and "r2" for the two instances of the registration table, and "m1" and "m2" for the members joining context.

SELECT
        t.id,
        t.activitie_id,
        m1.last_name LastName1,
        m1.first_name FirstName1,
        m2.last_name LastName2,
        m2.first_name FirstName2
    FROM 
        tandems t
            LEFT join registrations r1
                ON t.registration_member_one = r1.id
                LEFT JOIN members m1
                    ON r1.member_id = m1.id
            LEFT join registrations r2
                ON t.registration_member_two = m2.id
                LEFT JOIN members m2
                    ON r2.member_id = m2.id
    WHERE 
        t.activitie_id = 3;

To help you on this and in the future... Although mentally done, I try to mentally draw out how do I get the pieces together from the first table downstream. This can be seen too by the visual indentation almost like a tree view extension from T to R1 to M1, then R2 to M2 is a different branch. I also prefer to list the left table/alias.column = right table/alias.column in the join condition. How does T get to R1, then how does R1 get to M1.

In this, I used LEFT JOIN to each respective registration and member -- just-in-case only one person registered and a second may be pending. Not sure how your registration is actually structured.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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