Mehar
Mehar

Reputation: 2238

Retrieve data from tables having many-to-many relationships?

table 1

table 2

table 3

Now how i select the project_title from table2 having tm_id=10

and what is the best way to do this task?

Upvotes: 0

Views: 64

Answers (5)

medina
medina

Reputation: 8159

You could use this approach:

SELECT otp.project_title
FROM online_team ot
INNER JOIN online_team_projects otp USING (project_id)
WHERE ot.tm_id = 10

Upvotes: 0

Anand Shah
Anand Shah

Reputation: 633

use the query as below

 SELECT table2.project_title  from table2,table3 where table2.project_id = table3.project_id and table3.tm_id = 10

Upvotes: 1

Deval Shah
Deval Shah

Reputation: 1094

SELECT
  otp.project_title
FROM online_team_projects otp,
  team_project tp
WHERE otp.project_id = tp.project_id
    AND tp.tm_id = 10

Upvotes: 0

John Woo
John Woo

Reputation: 263703

I think a simple INNER JOIN will suffice your need.

SELECT  a.*, c.project_title
FROM    Online_team a
        INNER JOIN team_project b
            ON a.tm_id = b.tm_id
        INNER JOIN online_team_projects c   
            ON b.project_ID = c.project_ID
WHERE   a.tm_id = 10

To further gain more knowledge about joins, kindly visit the link below:

But if you don't need any columns from Online_team, you can remove it from the joins list.

SELECT  c.project_title
FROM    team_project b
        INNER JOIN online_team_projects c   
            ON b.project_ID = c.project_ID
WHERE   b.tm_id = 10

Upvotes: 1

Ian Atkin
Ian Atkin

Reputation: 6346

SELECT t2.project_title FROM table2 AS t2
JOIN table3 AS t3 ON t3.project_id = t2.project_id
WHERE t3.tm_id = 10;

Upvotes: 2

Related Questions