1

I want to return defined results from member_network table using WHERE clause by team_id = 91 using JOIN.

Main table member_network structure look like:

| id |  network_profile_name |                              
|----------------------------|
|  1 |    John Doe           |

I have two fields in two more connected tables with the values I need.

Table team_member_network structure looks like:

| id | team_member_id | member_network_id |
|----|----------------|-------------------|
|  2 |             1  |                 1 |

Table team_member:

| id    | team_id | member_id |
| ------|---------|-----------|
| 1     |      91 |   1679817 |   

This is some kind of reverse relationship

My work so far:

    SELECT
        network_profile_name
    FROM member_network
    LEFT JOIN team_member_network ON team_member_network.team_member_id = team_member.team_id
     WHERE team_id = 91;

With an error:

missing FROM-clause entry for table "team_member" LINE 9: team_member_network ON team_member_network.team_member_id = team_member

What is problem here is that I do not know how to select teamId in member_network table as it is located in team_member table which I need to get through team_member_network.team_member_id and get to team_member.team_id

Should I use one more JOIN?

O. Jones
  • 103,626
  • 17
  • 118
  • 172
  • 3
    Yes, just join all the tables you're interested in... Best advice for "should I try x?" is always to try it yourself first, rather than asking if you should try it... – MatBailie Aug 16 '22 at 10:10
  • 2
    Note that tables have _columns_, not fields. – jarlh Aug 16 '22 at 10:29
  • Please do not [delete & repost](https://stackoverflow.com/q/73363820/3404097) questions, edit them. Don't make us repeat the same reading & commenting effort. Poorly received questions count towards asking limits whether deleted or not. Again: [mre] PS Your "how to" "problem" statement is not clear. What do you mean, "get through" & "get to"? PS [Is there any rule of thumb to construct SQL query from a human-readable description?](https://stackoverflow.com/a/33952141/3404097) – philipxy Aug 16 '22 at 10:41
  • What is your 1 specific researched non=duplicate question? Please either ask about 1 bad query/function with the obligatory [mre] & why you think it should return something else at the 1st subexpression that it doesn't give what you expect justified by reference to authoritative documentation or ask about your overall goal giving working parts you can do & ideally a [mre]. But please ask about the former 1st because misconceptions in the former will get in the way of understanding the latter. And bad code doesn't tell us what you wish it would do. – philipxy Aug 16 '22 at 10:44
  • 1
    Possible duplicate of [Required to join 2 tables with their FKs in a 3rd table](https://stackoverflow.com/q/23839392/3404097) – philipxy Aug 16 '22 at 10:45
  • "defined results from member_network table using WHERE clause by team_id = 91" does not make sense. There is no "team_id" in that table. What are "defined results"? Use enough words, sentences & references to parts of examples to clearly & fully say what you mean. Not just a few words. Say enough that someone could go away & come back with a query. What were you told to write a query to do? Can you quote what you were told? When giving a business relation(ship)/association or table (base or query result), say what a row in it states about the business situation in terms of its column values. – philipxy Aug 16 '22 at 10:55
  • 1
    I deleted previous one as I wanted better to define what problem I have regarding this sql query. But thanks for this extended explanation. I will try to be more precise in the future. @philipxy – Filip Stojavonic Aug 16 '22 at 11:22

2 Answers2

2
 SELECT
        network_profile_name
    FROM member_network as mn
    LEFT JOIN team_member_network tmn ON mn.id = tmn.member_network_id
    LEFT JOIN team_member tm ON tm.id = tmn.team_member_id
     WHERE team_id = 91;

should work

Nickname_used
  • 430
  • 3
  • 18
  • 1
    Thanks a lot! I saved this question so I can learn more about this type of relationship so I can implement it on my own in the future. @Nickname_used – Filip Stojavonic Aug 16 '22 at 11:32
1

These three tables are part of a common data design pattern: many-to-many relationships. In your case you have a many-to-many relationship between your member_network entities (rows) and your team_member entities.

Your team_member_network table is sometimes called a join table, because it represents how to join your entities. It holds the relationships.

To use such a many-to-many relationship you do this:

SELECT member_network.network_profile_name,
       team_member.team_id
  FROM member_network
  JOIN team_member_network
        ON member_network.id = team_member_network.member_network_id
  JOIN team_member
        ON team_member_network.team_member_id = team_member.id
 WHERE team_member.team_id = 91;

The overall pattern is this:

    FROM EntityA
    JOIN JoinTable ON EntityA's ID
    JOIN EntityB ON EntityB's ID

If you want to see rows from your first table (member_network) without matching rows in your second table (team_member) use LEFT JOINs instead of ordinary inner JOINs.

O. Jones
  • 103,626
  • 17
  • 118
  • 172