转行
DROP TABLE IF EXISTS `temp_datalist`;
CREATE TEMPORARY TABLE temp_datalist
select distinct
a.id,
substring_index(substring_index(a.desc, ',', b.help_topic_id + 1), ',',-1) cd
from
table a
join mysql.help_topic as b
on b.help_topic_id < (char_length(a.desc) - char_length(replace(a.desc, ',', ''))+ 1)
where
type = 'A'
and sub_type = 'B';
转列(此例使用【|】进行分割)
select
a.id,
a.desc,
substring_index(substring_index(replace(a.desc,'补充|',''),'|',1),'|',-1) a,
substring_index(substring_index(replace(a.desc,'补充|',''),'|',2),'|',-1) b,
substring_index(substring_index(replace(a.desc,'补充|',''),'|',3),'|',-1) c,
substring_index(substring_index(replace(a.desc,'补充|',''),'|',4),'|',-1) d
from
table a
JSON化
JSON_ARRAYAGG(JSON_OBJECT('name', ifnull(c.code,''))) jsontxt
本文介绍了一种使用SQL进行数据处理的方法,包括如何通过临时表去除重复数据、将特定字符分隔的数据转换为多列以及将记录JSON化。这些技巧对于处理复杂数据结构尤其有用。
475

被折叠的 条评论
为什么被折叠?



