0

我无法走上正确的轨道,任何帮助将不胜感激

我有一张桌子

+---+----------+---------+-----------+
|id | match_id | team_id | player_id |
+---+----------+---------+-----------+
| 1 |        9 |      10 |         5 |
| 2 |        9 |      10 |         7 |
| 3 |        9 |      10 |         9 |
| 4 |        9 |      11 |        12 |
| 5 |        9 |      11 |        15 |
| 6 |        9 |      11 |        18 |
+---+----------+---------+-----------+

我想用 match_id 上的 where 和两个团队 id 选择这些,所以输出将是

+---------+-------+------+---------+---------+
| MATCHID | TEAMA | TEAMB| PLAYERA | PLAYERB |
+---------+-------+------+---------+---------+    
|       9 |    10 |   11 |       5 |      12 |
|       9 |    10 |   11 |       7 |      15 |
|       9 |    10 |   11 |       9 |      18 |
+---------+-------+------+---------+---------+

这可能很简单,但我被卡住了..

提前致谢

ps好像在我的第一篇文章中忘记了一个专栏,对不起

4

3 回答 3

2

我认为你需要:

SELECT
    a.match_id, a.team_id AS TeamA, b.team_id AS teamB, 
    a.player_id AS PlayerA, b.player_id AS PlayerB
FROM PLayer AS a
    INNER JOIN Player AS b ON a.match_id = b.match_id
WHERE a.team_id < b.team_id

虽然这会给你每场比赛的每一对球员,即

+---------+-------+------+---------+---------+
| MATCHID | TEAMA | TEAMB| PLAYERA | PLAYERB |
+---------+-------+------+---------+---------+    
|       9 |    10 |   11 |       5 |      12 | 
|       9 |    10 |   11 |       5 |      15 |
|       9 |    10 |   11 |       5 |      18 |
|       9 |    10 |   11 |       7 |      12 |
|       9 |    10 |   11 |       7 |      15 |
|       9 |    10 |   11 |       7 |      18 |
|       9 |    10 |   11 |       9 |      12 |
|       9 |    10 |   11 |       9 |      15 |
|       9 |    10 |   11 |       9 |      18 |
+---------+-------+------+---------+---------+

为了进一步限制它,您需要一个标准来确定玩家应该配对。

于 2009-12-12T14:06:16.963 回答
1

我认为最好重新设计您的数据库。

于 2009-12-12T14:06:18.303 回答
0
select MatchA.id as MATCHID, MatchA.team_id as TEAMA, MatchB.team_id as TEAMB, MatchA.player_id as PLAYERA, MatchB.player_id as PLAYERB
from Match as MatchA, match as MatchB 
where MatchA.id = MatchB.id and MatchA.team_id < MatchB.team_id
于 2009-12-12T14:07:17.727 回答