2

数据

+----+-------+------+--------+----+----+----------+------------+----------+----------+
| ID | CHESS | NAME | GROUPC | C2 | C3 | SECTION  | GROUPPOINT | C2POINTS | C3POINTS |
+----+-------+------+--------+----+----+----------+------------+----------+----------+
|  1 | C100  | S1   |      1 |  0 |  1 | Section1 |          5 |        0 |        3 |
|  2 | C100  | S2   |      1 |  0 |  1 | Section1 |          5 |        0 |        5 |
|  3 | C100  | S3   |      1 |  1 |  1 | Section1 |          5 |        5 |        1 |
|  4 | C101  | S4   |      0 |  1 |  0 | Section3 |          0 |        3 |        0 |
|  5 | C102  | S5   |      0 |  1 |  0 | Section1 |          0 |        3 |        0 |
|  6 | C103  | S6   |      1 |  1 |  5 | Section2 |          1 |        1 |        0 |
|  7 | C103  | S7   |      1 |  1 |  0 | Section2 |          1 |        0 |        0 |
|  8 | C103  | S8   |      1 |  0 |  3 | Section2 |          1 |        0 |        5 |
|  9 | C105  | S9   |      0 |  0 |  0 | Section1 |          0 |        0 |        0 |
| 10 | C104  | S10  |      1 |  1 |  0 | Section3 |          3 |        3 |        0 |
| 11 | C104  | S11  |      1 |  0 |  0 | Section3 |          3 |        0 |        0 |
| 12 | C104  | S12  |      1 |  1 |  1 | Section3 |          3 |        1 |        3 |
| 13 | C107  | S13  |      1 |  1 |  1 | Section1 |          3 |        1 |        1 |
| 14 | C107  | S14  |      1 |  1 |  1 | Section1 |          3 |        1 |        1 |
| 15 | C107  | S15  |      1 |  1 |  1 | Section1 |          3 |        1 |        1 |
| 16 | C105  | S16  |      1 |  1 |  0 | Section3 |          0 |        5 |        0 |
| 17 | C105  | S17  |      1 |  1 |  0 | Section3 |          0 |        5 |        0 |
| 18 | C105  | S18  |      1 |  1 |  0 | Section3 |          0 |        5 |        0 |
| 19 | C109  | S19  |      0 |  1 |  0 | Section3 |          1 |        0 |        0 |
+----+-------+------+--------+----+----+----------+------------+----------+----------+

问题

我想找到一个部分的总分。如果 forChess的值相同:NameGroupPoint/ C2Points/ C3Points(取决于GROUPC/ C2/ C3)应该被分组。

我目前正在使用以下查询,但不知道如何按Nameand进行分组GroupPoints

select * from ( select s1.ID, s1.Name, 'Competition 1' as Competition, 
s1.GroupPoint as  Points from students s1 where SECTION='Section1' and   
ifnull(s1.GROUPC,'')<>'' and s1.GroupPoint<>0 
union
select s2.ID, s2.Name, 'Competition 2' as Competition, s2.C2Points as Points
from students s2 where s2.SECTION='Section1' and ifnull(s2.C2,'')<>'' and     
s2.C2Points<>0
union
select s3.ID, s3.Name, 'Competition 3' as Competition, s3.C3Points as Points
from students s3 where s3.SECTION='Section1' and ifnull(s3.C2,'')<>'' and 3.C3Points<>0
)t order by t.points desc, t.Competition ASC

请参考这个SqlFiddle

首选输出

问题是:如何按名称/点对结果进行分组,以便最终输出类似于:

 +----------+---------------+--+--------------+--------+
 | Section1 |               |  |              |        |
 +----------+---------------+--+--------------+--------+
 | ID       | NAME          |  | COMPETITION  | POINTS |
 | 1        | S1, S2, S3    |  | Competition1 | 5      |
 | 3        | S3            |  | Competition2 | 5      |
 | 2        | S2            |  | Competition3 | 5      |
 | 15       | S13, S14, S15 |  | Competition1 | 3      |
 | 5        | S5            |  | Competition2 | 3      |
 | 1        | S1            |  | Competition3 | 3      |
 | 15       | S15           |  | Competition2 | 1      |
 | 13       | S13           |  | Competition2 | 1      |
 | 14       | S14           |  | Competition2 | 1      |
 | 14       | S14           |  | Competition3 | 1      |
 | 15       | S15           |  | Competition3 | 1      |
 +----------+---------------+--+--------------+--------+
4

1 回答 1

1

如果添加GROUP BY Chess, GroupPoint,则可以使用连接名称GROUP_CONCAT()

我创建了一个简化的示例,应该可以满足您的要求(union为简单起见,我省略了):

简化查询

SELECT
  ID,
  GROUP_CONCAT(DISTINCT Name ORDER BY Name ASC SEPARATOR ", ") AS Name,
  "Competition 1" as Competition,
  GroupPoint AS Points
FROM students
WHERE Section = "Section1"
AND GROUPC IS NOT NULL
AND GroupPoint <> 0
GROUP BY Chess, GroupPoint

输出

+----+---------------+---------------+--------+
| ID | NAME          | COMPETITION   | POINTS |
+----+---------------+---------------+--------+
| 1  | S1, S2, S3    | Competition 1 | 5      |
| 13 | S13, S15, S15 | Competition 1 | 3      |
+----+---------------+---------------+--------+

sqlfiddle

其他备注

  1. 您的 SQL 中似乎有错误:您在哪里做ifnull(s3.C2,'')<>'',您可能的意思是:ifnull(s3.C3,'')<>''
  2. 正确的做法ifnull(<field>, '')<>''<field> IS NOT NULL

最终查询

综上所述,我认为您的最终查询应如下所示:

SELECT *
FROM
(
SELECT s1.ID,
GROUP_CONCAT(s1.Name ORDER BY s1.Name ASC SEPARATOR ', ') AS Name,
'Competition 1' AS Competition,
s1.GroupPoint AS Points
FROM students s1
WHERE SECTION='Section1'
AND s1.GROUPC IS NOT NULL
AND s1.GroupPoint<>0
GROUP BY s1.Chess, s1.GroupPoint
UNION
SELECT s2.ID,
GROUP_CONCAT(s2.Name ORDER BY s2.Name ASC SEPARATOR ', ') AS Name,
'Competition 2' AS Competition,
s2.C2Points AS Points
FROM students s2
WHERE s2.SECTION='Section1'
AND s2.C2 IS NOT NULL
AND s2.C2Points<>0
GROUP BY s2.Chess, s2.C2Points
UNION
SELECT s3.ID,
GROUP_CONCAT(s3.Name ORDER BY s3.Name ASC SEPARATOR ', ') AS Name,
'Competition 3' AS Competition,
s3.C3Points AS Points
FROM students s3
WHERE s3.SECTION='Section1'
AND s3.C3 IS NOT NULL
AND s3.C3Points<>0
GROUP BY s3.Chess, s3.C3Points
)t
ORDER BY t.points DESC, t.Competition ASC

sqlfiddle

于 2013-08-16T07:33:04.327 回答