2016-02-28 117 views
0

我有以下字符串156,12,99,12,14,17有时候这会更长和/或更短,并涉及字母和数字,例如cc,21,366, DDD。Mysql按分隔符分割字符串(列)为新行

理想的输出是 output

output

我有这个,给了我区划界定的数列:

select 
LENGTH(a.column) - LENGTH(REPLACE(a.column, ',', '')) + 1 AS numberofdelimit 
FROM <mydatabase>.<mytable> as a 
where a.id = 4; 

我也知道,这个代码将会给我划定的文字:

SUBSTRING_INDEX(a.column, ',', numbers.n) 

我努力把它放在一起把值放入新行。

+1

在'MySQL'中没有直接的做法。你可能必须编写'MySQL函数'才能做到这一点。使用这种用户定义的'mysql函数'有没有什么问题? – 1000111

+0

一种选择是使用类似[common_schema](https://common-schema.googlecode.com/svn/trunk/common_schema/doc/html/introduction.html)的东西,特别是[split_token](https:// common -schema.googlecode.com/svn/trunk/common_schema/doc/html/split_token.html)和[get_num_tokens](https://common-schema.googlecode.com/svn/trunk/common_schema/doc/html/get_num_tokens。 HTML)。 – wchiquito

回答

0

好吧,如果你删除了ID的需求,你可以做这样的事情:

create table `ids` (
    `id` int 
); 

create table `input` (
    str varchar(256) 
); 

insert into ids (`id`) values 
    (1), (2), (3), (4), (10), (20), (30), (40); 

insert into input (`str`) values ('1,2,10,30'); 

再搭配这样

select 
    * 
from 
    input inner join ids 
     on concat(',',str,',') rlike concat(',', id, ','); 

它需要你有一个表尽管如此。

但它可能是一个开始。