从具有多对多关系的表中检索数据?

时间:2013-05-29 05:58:45

标签: php mysql

table 1

table 2

table 3

现在我如何从table2选择project_title,其中tm_id = 10

执行此任务的最佳方式是什么?

5 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

我认为一个简单的INNER JOIN就足以满足您的需求。

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

要进一步了解联接,请访问以下链接:

但如果您不需要Online_team中的任何列,则可以将其从联接列表中删除。

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

答案 2 :(得分:1)

使用以下查询

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

答案 3 :(得分:0)

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

答案 4 :(得分:0)

你可以使用这种方法:

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