1

我已经成功实现了以下场景:

SELECT GROUP_CONCAT(coursecode) 
    FROM (
    SELECT coursecode FROM Table18
    WHERE regno = 'StudentXYZ' AND (ca_score + exam_score) <= 39

    UNION  

    SELECT coursecode FROM Table17
    WHERE regno = 'StudentXYZ' AND (ca_score + exam_score) <= 39
) s

WHERE coursecode 
NOT IN (
    SELECT coursecode 
    FROM Table18
    WHERE regno = 'StudentXYZ' 
    AND (ca_score + exam_score) >= 40

    UNION

    SELECT coursecode 
    FROM Table17
    WHERE regno = 'StudentXYZ' 
    AND (ca_score + exam_score) >= 40
    )

这很好用!下面是示例结果:

+----------------------------------------------------------------------------------------------------------+
| GROUP_CONCAT(coursecode)                                                                                 |
+----------------------------------------------------------------------------------------------------------+
| EDU222,EDU497,POS302,POS405,POS420,EDU224,EDU311,EDU312,EDU313,GST304,GST305,POS304,POS305,POS308,POS309 |
+----------------------------------------------------------------------------------------------------------+

但这就是我想要实现的目标:

+-------------------------------------------------+
| GROUP_CONCAT(coursecode)                        |
+-------------------------------------------------+
| EDU222, EDU497, POS302, POS405, POS420, EDU224, |
| EDU311, EDU312, EDU313, GST304, GST305, POS304, |
| POS305, POS308, POS309                          |
+-------------------------------------------------+

有什么建议么?或者更好的方法来解决这个挑战?

4

1 回答 1

0

您可以执行以下操作(支持 MySQL 5.7 / MariaDB < 10.2):

SELECT GROUP_CONCAT(coursecode)
FROM (
    SELECT coursecode, @gn:=@gn+1, CEIL(@gn / 6) gn
    FROM (
        SELECT coursecode FROM Table18
        WHERE regno = 'StudentXYZ' AND (ca_score + exam_score) <= 39

        UNION  

        SELECT coursecode FROM Table17
        WHERE regno = 'StudentXYZ' AND (ca_score + exam_score) <= 39
    ) s, (SELECT @gn:=0)gn
    WHERE coursecode NOT IN (
        SELECT coursecode 
        FROM Table18
        WHERE regno = 'StudentXYZ' AND (ca_score + exam_score) >= 40

        UNION

        SELECT coursecode 
        FROM Table17
        WHERE regno = 'StudentXYZ' AND (ca_score + exam_score) >= 40
    )
) group_view GROUP BY group_view.gn

dbfiddle.uk 上的演示

从 MySQL 8.0 / MariaDB 10.2 开始,您可以使用相同的逻辑,但使用ROW_NUMBER而不是变量。因此,您的查询可能如下所示:

SELECT GROUP_CONCAT(coursecode)
FROM (
    SELECT coursecode, CEIL(ROW_NUMBER() OVER (ORDER BY code ASC) / 6) gn
    FROM (
        SELECT coursecode FROM Table18
        WHERE regno = 'StudentXYZ' AND (ca_score + exam_score) <= 39

        UNION  

        SELECT coursecode FROM Table17
        WHERE regno = 'StudentXYZ' AND (ca_score + exam_score) <= 39
    ) s
    WHERE coursecode NOT IN (
        SELECT coursecode 
        FROM Table18
        WHERE regno = 'StudentXYZ' AND (ca_score + exam_score) >= 40

        UNION

        SELECT coursecode 
        FROM Table17
        WHERE regno = 'StudentXYZ' AND (ca_score + exam_score) >= 40
    )
) group_view GROUP BY group_view.gn

dbfiddle.uk 上的演示

于 2019-08-13T17:47:44.700 回答