在不改变源数据库的情况下清理MySQL查询字符串数据?

在不改变源数据库的情况下清理MySQL查询字符串数据?,mysql,string,formatting,Mysql,String,Formatting,因此,我的MySQL数据库中有一些字段是字符串,我需要清理它们以便在查询中更具可读性 例如,我想要这个: name | interests ---------------- abc | a:2:{i:0;s:9:"Education";i:1;s:6:"Health";} xyz | a:3:{i:0;s:9:"Education";i:1;s:15:"Humanitarianism";i:2;s:33:"Sustainability and Climate Change";} 看起来像这

因此,我的MySQL数据库中有一些字段是字符串,我需要清理它们以便在查询中更具可读性

例如,我想要这个:

name | interests
----------------
 abc | a:2:{i:0;s:9:"Education";i:1;s:6:"Health";}
 xyz | a:3:{i:0;s:9:"Education";i:1;s:15:"Humanitarianism";i:2;s:33:"Sustainability and Climate Change";}
看起来像这样:

name | interests
----------------
 abc | "Education"; "Health";
 xyz | "Education"; "Humanitarianism"; "Sustainability and Climate Change";
不改变源数据库


非常感谢您的帮助

RiggsFolly的建议几乎肯定更好,但只是为了好玩,这里有一个粗糙而不完整的解决方案,使用MySQL中的一个helper表(一个整数(i)[0-9]的表(ints))


我花了很多时间才发现有人在谈论REGEXP\u REPLACE。我的mySQL参考网站上都没有列出它

所以,我用了

REGEXP_替换(table.values,'[a-z]:[0-9]*[[:punt:]]','')作为兴趣
a:2:{i:0;s:9:“教育”;i:1;s:6:“健康”}
是一个序列化数组。它是由PHP完成的。因此,在处理它之前,您必须使用PHP对其进行非序列化
DROP TABLE IF EXISTS my_table;

CREATE TABLE my_table
(id CHAR(3) PRIMARY KEY
,attributes VARCHAR(255) NOT NULL
);

INSERT INTO my_table VALUES ('xyz','a:3:{i:0;s:9:"Education";i:1;s:15:"Humanitarianism";i:2;s:33:"Sustainability and Climate Change";}');

SELECT x.id,GROUP_CONCAT(DISTINCT SUBSTRING_INDEX(SUBSTRING_INDEX(x.attributes,'"',(i+1)*2),'"',-1) SEPARATOR ';')n FROM my_table x,ints GROUP BY x.id;
+-----+----------------------------------------------------------------+
| id  | n                                                              |
+-----+----------------------------------------------------------------+
| xyz | Education;Humanitarianism;Sustainability and Climate Change;;} |
+-----+----------------------------------------------------------------+