简体   繁体   English

多个group_concat

[英]Multiple group_concat

I am in the process of planning a database for a mysql project and think I need to get this solved before I go any further. 我正在为mysql项目规划数据库,并且认为我需要解决这个问题,然后再进行下一步。

CREATE TABLE ResultsTbl (
    EventID INTEGER,
    MatchNumber INTEGER (9),
    TeamNumber int,
    IndividualName CHAR (100),
    Result char (4)
);

INSERT INTO ResultsTbl VALUES (1,1,1,'individual 1','W');
INSERT INTO ResultsTbl VALUES (1,1,1,'individual 2','W');
INSERT INTO ResultsTbl VALUES (1,1,2,'individual 3','L');
INSERT INTO ResultsTbl VALUES (1,1,2,'individual 4','L');
INSERT INTO ResultsTbl VALUES (1,1,3,'individual 5','L');
INSERT INTO ResultsTbl VALUES (1,1,3,'individual 6','L');
INSERT INTO ResultsTbl VALUES (1,2,1,'individual 7','W');
INSERT INTO ResultsTbl VALUES (1,2,2,'individual 8','L');
INSERT INTO ResultsTbl VALUES (1,3,1,'individual 9','W');
INSERT INTO ResultsTbl VALUES (1,3,1,'individual 10','W');
INSERT INTO ResultsTbl VALUES (1,3,2,'individual 11','L');
INSERT INTO ResultsTbl VALUES (1,3,2,'individual 12','L');

So I have a table which holds data for matches. 所以我有一张桌子,上面放着比赛用的数据。 I need the end result to have one cell for each MatchNumber where the Result = 'W' and one where the Result = 'L', but I need the IndividualNames on the same teams concatenated with a "&". 我需要最终结果为每个MatchNumber在结果='W'的情况下有一个单元格,而在结果='L'的情况下要有一个单元格,但是我需要在同一小组中将“ IndividualNames”与“&”串联起来。 I also need the teams concatenated with a ",". 我还需要将团队与“,”串联。

For the example above, the desired result needs to be as follows: 对于上面的示例,所需的结果需要如下:

MatchNumber  |  Winners                      |   Losers 
1            |  Individual 1 & individual 2  |  individual 3 & individual 4, individual 5 & individual 6
2            |  individual 7                 |  individual 8
3            |  individual 9 & individual 10 |  individual 11 & individual 12

So on MatchNumber 1 we have one team in the winners but 2 teams in the losers. 因此,在MatchNumber 1上,获胜者只有1支队伍,而输者有2支队伍。 The individual within the same teams are concatenated with a "&", and the different teams are concatenated with a ",". 同一团队中的个人以“&”连接,而不同团队中以“,”连接。 Match 2 only has 1 individual on each team, so there are no special characters needed. 第2场比赛每队只有1个人,因此不需要特殊角色。 Match 3 has 1 team of 2 individuals on both the winners and losers, so they are concatenated with a "&" with no need for a ",". 第3场比赛在获胜者和失败者上都有1个由2个人组成的小组,因此,它们用“&”串联,而无需使用“,”。

I would love to write what I have failed on but I just can't get any where near what I need to do. 我很想写自己失败的事情,但是我无法做到任何事情都需要做。 I have looked down the road of a group_concat on the teams, then a group_concat on the result, but I can't figure it out. 我在团队中看了一个group_concat,然后在结果上看了一个group_concat,但我无法弄清楚。

I wanted to get this dealt with before processing all of the data, so if the table structures need changing to get this done then I am open to suggestions. 我想在处理所有数据之前先解决此问题,因此,如果需要更改表结构以完成此操作,那么我愿意提出建议。

Thanks for any help or advice anyone can give. 感谢您提供任何帮助或建议。

You need a first set of results by match number, team, and winning type 您需要按比赛编号,球队和获胜类型划分的第一组结果
You need second set of result by match number and winning type 您需要按比赛编号和获胜类型获得第二组结果

   select 
      MatchNumber,
      group_concat( Winners ) Winners,
      group_concat( Losers ) Losers
   from
      (
         select
          MatchNumber,
          TeamNumber,
          case Result when 'W'
                  then group_concat(IndividualName separator '&') end Winners,
          case Result when 'L'
                  then group_concat(IndividualName separator '&') end Losers,
          group_concat(Result separator '&') r
         from
          Resultstbl 
         group by 
          MatchNumber, TeamNumber
      ) g 
   group by MatchNumber

Give this a bash: 给这个bash:

select MatchNumber,
  group_concat(Winners order by TeamNumber),
  group_concat(Losers order by TeamNumber)
from
(
 select MatchNumber,TeamNumber,
  group_concat(case when Result = 'W' then IndividualName end SEPARATOR " & ") as Winners,
  group_concat(case when Result = 'L' then IndividualName end SEPARATOR " & ") as Losers
  from ResultsTbl
 group by MatchNumber, TeamNumber
) t
group by MatchNumber
;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM