Mysql 字符串拆分 OR 一行轉多行
需要了解的的幾個mysql 函數:
A.substring_index():字符串截取 substring_index(str,delim,count)
str:要處理的字符串
delim:分隔符
count:計數
B.length():字符長度函數
C.replace():替換函數
不說了,直接看代碼:
SELECT substring_index(substring_index( a.chain,'_',b.help_topic_id + 1 ), '_' ,- 1 ) AS ID FROM (select '1_11_1223_1242' as chain) a JOIN mysql.help_topic b ON b.help_topic_id < (length(a.chain) - length( replace(a.chain, '_', '') ) + 1)
結果👇:
首先如果有疑問,可以在數據庫進行測試,一步一步進行推斷執行,你就會明白了,實踐是最好的學習成長捷徑。