工作中,mysql字符串排序如何做?

1.问题由来

如下图所示: 我们的需求是根据 后缀的数字排序,因为不是纯数字所以常用的解决方案例如,order by filed +1 ,或者是 order by CAST(server_id as SIGNED) desc 这种方案都是不可取的。

 

2.解题思路

   表结构: 

默认数据:

   2.1定义规范

    和客户(划重点沟通很重要)探讨之后,客户说后面的数字 最多是三个,最少2个。

    也是就 数据只会出现有 

     例1:  兴城10-1                   V
     例2:  兴城10-101-123        V

     反例1:兴城10                      X
     反例2:新城10-1-123-13      X

   2.2利用mysql 字符串拆分机制

   示例数据:兴城10-101-123

   1.获取第一个数字的位置 拆分成  兴城  和  10-101-123

   利用 find_first_int(自定义的寻找第一个数字位置函数), 和 字符串截取

CREATE DEFINER = 'root'@'localhost' FUNCTION `find_first_int`(
        pData CHAR(60)
    )
    RETURNS int(11)
    NOT DETERMINISTIC
    CONTAINS SQL
    SQL SECURITY DEFINER
    COMMENT ''
BEGIN
  DECLARE vPos INT DEFAULT 1;
  DECLARE vRes INT DEFAULT 0;
  DECLARE vChar INT;
  WHILE vPos <= LENGTH(pData) DO
    SET vChar = ASCII(SUBSTR(pData,vPos,1));
    IF vChar BETWEEN 48 AND 57 THEN
      RETURN vPos;
    END IF;
    SET vPos = vPos + 1;
  END WHILE;
  RETURN NULL;
END;
 select test,substring(test,find_first_int(test)) 截取后的 from `testtable`;

    

  2.把截取后的 分为 one ,tow ,three 三个字段,兴城10-101-123 对应的 onw = 10,two = 101,three = 123

select test,
LEFT(substring(test,Locate('1',test)  ),(LOCATE('-',substring(test,find_first_int(test)))-1)) one,

if(length(test)-length(replace(test,'-','')) = 1,
substring(substring(test,find_first_int(test)  ),(LOCATE('-',substring(test,find_first_int(test)))+1)) 
,
left(substring(substring(test,find_first_int(test)  ),(LOCATE('-',substring(test,find_first_int(test)))+1)),LOCATE( '-',substring(substring(test,find_first_int(test)  ),(LOCATE('-',substring(test,find_first_int(test)))+1)))-1 )
)two,

right(substring(substring(test,find_first_int(test)  ),(LOCATE('-',substring(test,find_first_int(test)))+1)),LOCATE( '-',substring(substring(test,find_first_int(test)  ),(LOCATE('-',substring(test,find_first_int(test)))+1)))-1 ) three

from `testtable`;

3.最后 order by one,two,three 就可以得到想要的排序结果拉

select test
from `testtable`
order by 
LEFT(substring(test,find_first_int(test)  ),(LOCATE('-',substring(test,find_first_int(test)))-1)) *1,
if(length(test)-length(replace(test,'-','')) = 1,
substring(substring(test,find_first_int(test)  ),(LOCATE('-',substring(test,find_first_int(test)))+1))*1 
,
left(substring(substring(test,find_first_int(test)  ),(LOCATE('-',substring(test,find_first_int(test)))+1)),LOCATE( '-',substring(substring(test,find_first_int(test)  ),(LOCATE('-',substring(test,find_first_int(test)))+1)))-1 ))*1
,
right(substring(substring(test,find_first_int(test)  ),(LOCATE('-',substring(test,find_first_int(test)))+1)),LOCATE( '-',substring(substring(test,find_first_int(test)  ),(LOCATE('-',substring(test,find_first_int(test)))+1)))-1 )*1
 ;

总结

工作中会遇到很多问题,沟通很重要,其次就是解题思路。

最后我准备发 获取 one ,two ,thrre 的地方小小封装一下,毕竟看上去有点  乱0 0,封装成函数会好点。

 

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值