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