由於很多業務表因爲歷史原因或者性能原因,都使用了違反第一範式的設計模式。即同一個列中存儲了多個屬性值(具體結構見下表)。
這種模式下,應用常常需要將這個列依據分隔符進行分割,並得到列轉行的結果。
表數據:
ID Value
1 tiny,small,big
2 small,medium
3 tiny,big
期望得到結果:
ID Value
1 tiny
1 small
1 big
2 small
2 medium
3 tiny
3 big
select a.ID,substring_index(substring_index(a.mSize,',',b.help_topic_id+1),',',-1)
from
tbl_name a
join
mysql.help_topic b
on b.help_topic_id < (length(a.mSize) - length(replace(a.mSize,',',''))+1)
order by a.ID;