有没有更好的方法将 MySQL GROUP_CONCAT 输出分成多行?例如说每六 (6) 条记录后
Is there a better way to split MySQL GROUP_CONCAT output into multiple rows? for example say after every six (6) record
我已经成功实现了下面这个场景:
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 |
+-------------------------------------------------+
有什么建议吗?或解决这一挑战的更好方法?
您可以执行以下操作(支持 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
从 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
我已经成功实现了下面这个场景:
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 |
+-------------------------------------------------+
有什么建议吗?或解决这一挑战的更好方法?
您可以执行以下操作(支持 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
从 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