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)

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM