SELECT @id:=@id+1 AS id,
`brand`,
`group`
FROM
(SELECT
`id`,
`brand`,
SUBSTRING_INDEX(SUBSTRING_INDEX(`group`, ',', n.digit+1), ',', -1) AS `group`
FROM
Table1
INNER JOIN
(SELECT 0 AS digit
UNION ALL SELECT 1
UNION ALL SELECT 2
UNION ALL SELECT 3
UNION ALL SELECT 4
UNION ALL SELECT 5
UNION ALL SELECT 6) n
ON LENGTH(REPLACE(`group`, ',' , '')) <= LENGTH(`group`)-n.digit
Order by ID,`group`) AS T,(SELECT @id:=0) AS R;
select
brandstable.id,
brandstable.brand,
SUBSTRING_INDEX(SUBSTRING_INDEX( brandstable.`groupname`, ',', numbers.n), ',', -1) `groupname`
from
(select 1 n union all
select 2 union all select 3 union all
select 4 union all select 5) numbers INNER JOIN brandstable
on CHAR_LENGTH(brandstable.groupname)
-CHAR_LENGTH(REPLACE(brandstable.groupname, ',', ''))>=numbers.n-1
order by
id, n
replace your tablename with brandstable.
3条答案
按热度按时间rks48beu1#
输出
演示
http://sqlfiddle.com/#!9月29日C2月31日
有关解释,请转到此链接并查看我的答案
mysql:将列数据转换为行
jm2pwxwz2#
请使用php explode()函数。它可以将字符串拆分为数组。
语法:“explode(separator,string,limit);”
bq9c1y663#