0

我如何从该表中生成逗号和分号分隔的字符串

具有 3 个字段的结果表

GameId(key)  PlayerId(key)  Rank
1            1              1
1            2              2
1            3              3
1            4              4
1            5              5
2            1              2
2            2              3
2            3              1
2            4              5
2            5              4

我需要的结果接近于这样的 sql

SELECT PlayerId, count(*) as R1 from Result where Rank = 1
group by PlayerId
union
SELECT PlayerId, count(*) as R2 from Result where Rank < 5
group by PlayerId

分隔字符串应类似于 1,5,8;2,3,7; 这是PlayerId,R1,R2;

4

1 回答 1

0
SELECT GROUP_CONCAT(CONCAT_WS(',', PlayerId, R1, R2) SEPARATOR ';') FROM (
  SELECT   PlayerId, SUM(Rank=1) R1, SUM(Rank<5) R2
  FROM     Result
  GROUP BY PlayerId
) t

sqlfiddle上查看。

于 2013-07-10T12:44:07.333 回答