mysql查询连接三个表

时间:2013-07-15 19:07:34

标签: mysql sql

我有三张桌子:

players
------
id|name 

teams
-------
id|name 

teams_players
-------------
id|teamID|playerID

我现在想从桌面“玩家”中获取每个条目,其中players.id是例如在ID为15的团队中。换句话说:我想让每个属于特定团队的玩家(例如teamID = 15) )

我尝试了加入,但失败了。

这是我到目前为止所得到的:

       "SELECT players.*
        FROM players
        JOIN teams_players
        ON teams_players.teamID = 15 
        GROUP BY players.id"; 

2 个答案:

答案 0 :(得分:2)

你的方式过于复杂。

select players.*
 from players
 join teams_players
 on players.id = teams_players.playerid
 where teams_players.teamid = 15

JOIN部分与数字15无关.15是您过滤结果的方式。

答案 1 :(得分:0)

您可以使用多个条件加入您的表格,同时您还未在on子句中提及表格之间的关系

SELECT players.*
        FROM players
        JOIN teams_players
        ON (players.id = teams_players.playerid AND teams_players.teamID = 15 )
        GROUP BY players.id