mysql一行与多行之间转换

数据表

一行变多行

select a.classid, substring_index(substring_index(a.classname, ',', b.help_topic_id + 1), ',', -1) as spitId
 
from classroom a
 
join mysql.help_topic b on b.help_topic_id < (length(a.classname) - length(replace(a.classname, ',', '')) + 1)
 
where a.classid = 5;

运行效果

多行变一行

select classname ,group_concat(classid Separator ',') as score from classroom group by classname;

运行效果

原文地址:https://www.cnblogs.com/zyg316/p/14142687.html