#第一步:根据分号“;”分割为多行
#第二步:根据逗号“,”分割为多列
select distinct S1.tbox_vin,
(select substring_index(substring_index(S1.error_code, ',', 1), ',', -1)) as spn,
(select substring_index(substring_index(S1.error_code, ',', 2), ',', -1)) fmi,
S1.modify_time
from (
select t1.tbox_vin,
substring_index(substring_index(t1.dm1_string, ';', t2.help_topic_id + 1), ';', -1) as error_code,
t1.modify_time
from tonly_error_record t1
join mysql.help_topic t2
on t2.help_topic_id < (length(t1.dm1_string) - length(replace(t1.dm1_string, ';', '')) + 1)
where t1.dm1_string is not null
and t1.dm1_string != '') S1
where s1.error_code != ''
and s1.error_code is not null
order by S1.modify_time desc;
涉及的知识点
一、字符串拆分: SUBSTRING_INDEX(str, delim, count)
1. 参数解说
SELECT
SUBSTRING_INDEX(SUBSTRING_INDEX('7654,7698,7782,7788',',',help_topic_id+1),',',-1) AS num
mysql.help_topic
WHERE
help_topic_id < LENGTH('7654,7698,7782,7788')-LENGTH(REPLACE('7654,7698,7782,7788',',',''))+1
此处利用 mysql 库的 help_topic 表的 help_topic_id 来作为变量,因为 help_topic_id 是自增的,当然也可以用其他表的自增字段辅助。