mySQL SELECT join、group by 或其他

发布于 2024-08-15 04:43:33 字数 928 浏览 7 评论 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 和两个团队 id 上的位置来选择这些表,因此输出将是

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

这可能非常简单,但我卡住了..

提前

感谢似乎忘记了我第一篇文章的专栏,抱歉

I can't get on the right track with this, any help would be appreciated

I have one table

+---+----------+---------+-----------+
|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 |
+---+----------+---------+-----------+

I want to select these with a where on the match_id and both team id's so the output will be

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

It's probably very simple, but i'm stuck..

thanks in advance

p.s. seemed to forgot a column on my first post, sorry

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

又怨 2024-08-22 04:43:33

我认为你需要:

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 |
+---------+-------+------+---------+---------+

为了进一步限制它,你需要一个标准来确定球员应该配对。

I think you need:

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

Though this will give you every pair of players for each game, i.e.

+---------+-------+------+---------+---------+
| 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 |
+---------+-------+------+---------+---------+

To restrict it further, you need a criterion to determine players should be paired.

很快妥协 2024-08-22 04:43:33

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

I think it's better to redesign your database.

蓝海似她心 2024-08-22 04:43:33
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
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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文