对于所有的球员,我需要找到球员的号码和他们曾经参加过的球队的名单。
这是“火柴”表:

+---------+--------+----------+-----+------+
| MATCHNO | TEAMNO | PLAYERNO | WON | LOST |
+---------+--------+----------+-----+------+
|       1 |      1 |        6 |   3 |    1 |
|       2 |      1 |        6 |   2 |    3 |
|       3 |      1 |        6 |   3 |    0 |
|       4 |      1 |       44 |   3 |    2 |
|       5 |      1 |       83 |   0 |    3 |
|       6 |      1 |        2 |   1 |    3 |
|       7 |      1 |       57 |   3 |    0 |
|       8 |      1 |        8 |   0 |    3 |
|       9 |      2 |       27 |   3 |    2 |
|      10 |      2 |      104 |   3 |    2 |
|      11 |      2 |      112 |   2 |    3 |
|      12 |      2 |      112 |   1 |    3 |
|      13 |      2 |        8 |   0 |    3 |
+---------+--------+----------+-----+------+

我能想到的最好的办法是:
SELECT DISTINCT playerno, teamno
FROM matches
ORDER BY playerno;

结果是:
+----------+--------+
| playerno | teamno |
+----------+--------+
|        2 |      1 |
|        6 |      1 |
|        8 |      1 |
|        8 |      2 |
|       27 |      2 |
|       44 |      1 |
|       57 |      1 |
|       83 |      1 |
|      104 |      2 |
|      112 |      2 |
+----------+--------+

注意8号球员在两队的表现。我怎样才能让表格只显示球员8的一行和teamno的列表(1&2)?

最佳答案

您可以使用group_concat聚合函数:

SELECT   playerno, GROUP_CONCAT(DISTINCT teamno)
FROM     matches
GROUP BY playerno
ORDER BY playerno;

08-20 03:53