两个表上有两个连接

时间:2017-11-28 00:01:40

标签: mysql sql

我有这两张桌子。我想在我的real_name表格中加入player playerA来自我playerBmatch的{​​{1}}表。所以我尝试了这个。不幸的是,当我尝试这个时,我的匹配从46k记录到超过一百万条记录,所以我知道这不能正常工作。

SELECT players.player_id, players.real_name, matches.playerA, matches.playerB, matches.scoreA, matches.scoreB
  FROM players
  JOIN matches ON players.player_id = matches.playerA
  JOIN matches m1 ON players.player_id = m1.playerB;
+----------+---------+---------+
| match_id | playerA | playerB |
+----------+---------+---------+
|        1 |       4 |      55 |
|        2 |       2 |      41 |
|        3 |      21 |      41 |
|        4 |       3 |       2 |
|        5 |      41 |       2 |
|        6 |      21 |       3 |
|        7 |       1 |       8 |
|        8 |       1 |       8 |
|        9 |       8 |      19 |
|       10 |      19 |      12 |
+----------+---------+---------+
+-----------+-----------------+
| player_id | real_name       |
+-----------+-----------------+
|         1 | Dong Nyoung Lee |
|         2 | Hyun Woo Jang   |
|         3 | Seung Hyun Lee  |
|         4 | Soo Ho Park     |
|         5 | Lee Sak Won     |
|         6 | Young Suh Yoon  |
|         7 | Yoon Jong Jung  |
|         8 | Dong Hwan Kim   |
|         9 | Tae Hoon Kwon   |
|        10 | Ilyes Satouri   |
+-----------+-----------------+

我添加了要求的内容。

+-----------+------------------------------------+---------+---------+--------+--------+
| player_id | real_name                          | playerA | playerB | scoreA | scoreB |
+-----------+------------------------------------+---------+---------+--------+--------+
|         4 | Soo Ho Park                        |       4 |      55 |      1 |      3 |
|         4 | Soo Ho Park                        |       4 |      55 |      1 |      3 |
|         4 | Soo Ho Park                        |       4 |      55 |      1 |      3 |
|         4 | Soo Ho Park                        |       4 |      55 |      1 |      3 |
|         4 | Soo Ho Park                        |       4 |      55 |      1 |      3 |
|         4 | Soo Ho Park                        |       4 |      55 |      1 |      3 |
|         4 | Soo Ho Park                        |       4 |      55 |      1 |      3 |
|         4 | Soo Ho Park                        |       4 |      55 |      1 |      3 |
|         4 | Soo Ho Park                        |       4 |      55 |      1 |      3 |
|         4 | Soo Ho Park                        |       4 |      55 |      1 |      3 |
|         4 | Soo Ho Park                        |       4 |      55 |      1 |      3 |
|         4 | Soo Ho Park                        |       4 |      55 |      1 |      3 |
|         4 | Soo Ho Park                        |       4 |      55 |      1 |      3 |
|         4 | Soo Ho Park                        |       4 |      55 |      1 |      3 |
|         4 | Soo Ho Park                        |       4 |      55 |      1 |      3 |
|         4 | Soo Ho Park                        |       4 |      55 |      1 |      3 |

2 个答案:

答案 0 :(得分:4)

如果你的目的是获得一排/匹配,我相信你想要的是:

SELECT p1.player_id, p1.real_name, p2_player_id, p2_player_name, 
     m.scoreA, m.scoreB
FROM matches m
JOIN players p1 ON p1.player_id = m.playerA
JOIN players p2 ON p2.player_id = m.playerB

实际情况是,你参加的每个比赛选手的排数乘以每个参赛选手参加的比赛,参加表'比赛'中的每个配对。

答案 1 :(得分:2)

显示之前运行的查询以及要显示的数据会很有帮助。

您的查询当前正在做的是,对于每个玩家,从他们是玩家A的每个匹配中加载行,然后加载他们是玩家B的每个匹配。因为SQL没有"#34;行向上"这两个连接,而是交叉倍增它们(即生成它们的每个组合),你可能会为每个玩家ID生成许多行。

如果你所追求的是匹配ID的表格,其中包含参赛者的全名和分数,那就更像是这样:

select m.match_id, pA.full_name, m.scoreA, pB.full_name, m.scoreB from matches m join players pA on pA.player_id = m.playerA join players pB on pB.player_id = m.playerB