我怎么能从这张表产生一个逗号和分号分隔的字符串

结果表包含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;

最佳答案

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上查看。

10-05 21:32