按名称和点对结果进行分组

时间:2013-08-16 05:16:37

标签: php mysql

数据

+----+-------+------+--------+----+----+----------+------------+----------+----------+
| 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 |
+----+-------+------+--------+----+----+----------+------------+----------+----------+

问题

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

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

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      |
 +----------+---------------+--+--------------+--------+

1 个答案:

答案 0 :(得分: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
  3. 最终查询

    考虑到以上所有情况,我认为您的最终查询应如下所示:

    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