2011-06-06 78 views
0

我在MySQL表格中有一列是格式为csv的中文文本。我有兴趣提取包含字符'*'的这些csv列表的成员。什么是最简单的方法来做到这一点?如何提取包含特定字符的MySQL表中csv列的成员?

+0

要澄清,你有一个字段的值是:“一”,“二”,“三*”,你想只是检索“三*”?或者你想检索整个记录? – dmcnelis 2011-06-06 17:21:10

+0

我只想检索“三*”。本专栏和许多行都有大量的文字。我正试图专注于本专栏中这些csv列表的这些成员。 – jonderry 2011-06-06 17:23:25

回答

1

我从来没有做过类似的事情。正如你所看到的,我做了一个简单的测试。 我不知道大记录集的性能。

create table mytest (
id int not null auto_increment primary key, 
content varchar(100) 
) engine = myisam; 

insert into mytest (content) 
values 
('one,two*,three*text'), 
('four,five'), 
('six*,seven*,eight*'); 



delimiter // 
drop procedure if exists split_found // 
create procedure split_found() 
begin 
declare str varchar(200); 
declare ssql varchar(200); 
declare finito int default 0; 
declare cursore cursor for select content from mytest; 
declare continue handler for not found set finito = 1; 
drop temporary table if exists tmp; 
create temporary table tmp (cont varchar(50)); 
open cursore; 
mio_loop:loop 
fetch cursore into str; 
if finito = 1 then 
leave mio_loop; 
end if; 
set @ssql = concat("insert into tmp (cont) values ('", replace(str, ',', "'),('"), "')"); 
prepare stmt from @ssql; 
execute stmt; 
deallocate prepare stmt; 
end loop; 
close cursore; 
select * from tmp where cont like '%*%'; 
end; // 
delimiter ; 

mysql> call split_found(); 
+------------+ 
| cont  | 
+------------+ 
| two*  | 
| three*text | 
| six*  | 
| seven*  | 
| eight*  | 
+------------+ 
5 rows in set (0.01 sec) 
+0

这个简单的例子工作,但我得到的错误,当我尝试在我感兴趣的数据库中使用它。例如,ERROR 1436(HY000):线程堆栈溢出:131072 131072一字节堆栈的使用5512个字节,需要的字节。使用'mysqld -O thread_stack =#'指定一个更大的堆栈。 – jonderry 2011-06-06 23:49:50

相关问题