第一步:首先查看你所操作的表是否有一列自增的id列,如果是随机字符串表示的id列,还需要把表查询一次变成含有id自增列的表,具体如下所示:
没有自增id前:
SELECT * from t_xfcf_tq as t
加了自增id后:
SELECT @rownum := @rownum+1 AS id,t.XH, t.CFXFBHLB
FROM (SELECT @rownum:=0)r , t_xfcf_tq as t
第二步:查询表有自增列之后再吧对单独的带有逗号的列,拆分为多行。
SELECTa.id,substring_index(substring_index(a.CFXFBHLB,',',b.help_topic_id + 1),',' ,- 1) AS CFXFBHLB,a.XHFROM (SELECT @rownum := @rownum+1 AS id,t.XH, t.CFXFBHLB FROM (SELECT @rownum:=0)r , t_xfcf_tq as t ) aJOIN mysql.help_topic b ON b.help_topic_id < (length(a.CFXFBHLB) - length(REPLACE (a.CFXFBHLB, ',', '')) + 1)
特别注意:b.help_topic_id 不要动是虚拟表
SELECT(查询表的自增id),substring_index(substring_index((需要分割的列),',',b.help_topic_id + 1),',' ,- 1) AS 别名,(其他字段名)FROM(SELECT @rownum := @rownum+1 AS id,(其他字段名), (需要分割的列)FROM (SELECT @rownum:=0)r , (你的表) as t) a//下面一行尽量不要改动,只需要改2个地方就是length()和REPLACE()里面的//a.CFXFBHLB也就是你的表中需要分割的列名字。JOIN mysql.help_topic b ON b.help_topic_id < (length(需要分割的列) - length(REPLACE (需要分割的列, ',', '')) + 1)