不同表上的多个连接

时间:2014-07-24 10:46:26

标签: mysql sql

我有3张桌子

 1. player_list
 2. tournaments
 3. rank_list

包含以下字段

player_list

- id, player_id, tournament

比赛

- id, tournament, rl_month

rank_list

- id, player_id, rl_month, rl_position

我想构建一个查询,以便从rl_position表格上的rl_month表中查找特定锦标赛的每个player_id的特定rank_list player_list (player_list.tournament = tournaments.id)

FK (rl_month)tournaments之间还有一个rank_list

有什么想法吗?

感谢。

2 个答案:

答案 0 :(得分:1)

由于FK不是唯一的,因此使得这个技巧变得有点慢的查询是:

select r.id, r.rl_year, t.code, t.tournament, t.rl_month, 
r.rl_pos, r.rl_pts, p.player_id, p.name, p.gender, p.assoc, p.cat
from fab_plist p 
inner join fab_rl r 
on p.player_id = r.player_id
inner join fab_tournaments t 
on p.tournament = t.id where r.rl_pos is not null 
AND p.tournament = t.id AND r.rl_month = 
(select fab_tournaments.rl_month from fab_tournaments where fab_tournaments.id = t.id)
order by r.rl_pos;

答案 1 :(得分:0)

试试这个

select rl_position  from rank_list r 
inner join player_list p 
on p.player_id = r.player_id
inner join tournaments t p.tournament =t.id and t.rl_month='Assign month'