数据库中有排序字段(varchar类型)内容是:
1
1.1
2
2.1
2.1.1
10.1
想到的解决办法是将该字段分隔合并:
如
1.1分隔合并成001001
2分隔合并成002
10.1分隔合并称010001
然后升序排序即可。
附实现函数
create definer=`root`@`localhost` function `split_pad`(str varchar (1000),delimiter varchar(1)) returns varchar(200)
begin
declare cur_str varchar(200);
declare sub_len int;
declare sub_str varchar(200);
declare ret_str varchar(200);
declare loop_count int;
set cur_str = str;
set sub_len = -1;
set ret_str = '';
set loop_count = 0;
repeat
set cur_str = substring(cur_str,sub_len + 2);
set sub_str = substring_index(cur_str,delimiter,1);
set sub_len = length(sub_str);
if sub_len !=0 then
set ret_str = concat(ret_str,lpad(sub_str,3,'0'));
end if;
set loop_count = loop_count + 1;
until sub_len = 0
end repeat;
return ret_str;
end;