简体   繁体   中英

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

表格1

表2

表3

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

and what is the best way to do this task?

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

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

使用如下查询

 SELECT table2.project_title  from table2,table3 where table2.project_id = table3.project_id and table3.tm_id = 10
SELECT
  otp.project_title
FROM online_team_projects otp,
  team_project tp
WHERE otp.project_id = tp.project_id
    AND tp.tm_id = 10

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

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