4

我有两张桌子:

teams
----------------
|uid|name |rank|
----------------
|  1  |Team1|  1  |
|  2  |Team2|  2  |
----------------

games
-----------------------------------------------------------------------
|uid|team_one_uid|team_one_score|team_two_uid|team_two_score|game_date|
-----------------------------------------------------------------------
|1|1|70|2|50|2012-12-12|

团队表有一个团队列表和其他数据,如排名。游戏表有一个游戏列表,并通过其唯一 ID (uid) 引用每个团队。我可以运行什么查询来查看包含具有以下列的行的结果:

game_uid, team_one_name, team_one_rank, team_one_score, team_two_name, team_two_rank, team_two_score, game_date
4

2 回答 2

12
select g.uid as game_uid, 
       t1.name as team_one_name, 
       t1.rank as team_one_rank, 
       team_one_score, 
       t2.name as team_two_name, 
       t2.rank as team_two_rank, 
       team_two_score, 
       game_date
from games g 
inner join teams t1 on t1.uid = team_one_uid
inner join teams t2 on t2.uid = team_two_uid
于 2012-08-07T20:18:59.613 回答
1

我想这就是你要找的:

SELECT g.uid AS game_uid, t1.name AS team_one_name, t1.rank AS team_one_rank,
g.team_one_score, t2.name AS team_two_name, t2.rank AS team_two_rank, 
g.team_two_score, g.game_date 
FROM `games` g, `teams` t1, `teams` t2 
WHERE t1.id = g.team_one_uid 
AND t2.id = g.team_two_uid

这也可以完成,INNER JOIN但它达到了同样的目的。

于 2012-08-07T20:20:55.793 回答