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;

运行效果

mysql合并行用到GROUP_CONCAT函数,但有时会出现编码问题导致查询出的是blob数据类型,这时候需要编码转换一下便可解决

转换方式:CONVERT(GROUP_CONCAT(需要转换的字段名) USING utf8)

 

posted @ 2020-12-16 10:42  小飛  阅读(1632)  评论(0编辑  收藏  举报