1. 程式人生 > 其它 >MYSQL 提取字串中的數字

MYSQL 提取字串中的數字

方法一:

目標表contract_service_content資料例項

 

 配置表configdb的資料例項,如果有許可權訪問資料庫的  mysql.help_topic 表 則不用建立配置表 直接用mysql.help_topic表中的help_topic_id,不然就要建立一個表ID連續的配置表

 

 

select spc_id,fee,cast(group_concat(c order by pos separator '') as unsigned)
         as mixed1
  from (
    /*分割fee字串 並將將字元的asc碼在48到57之間的提取出來(也就是數字)
*/ select v.spc_id , v.fee, iter.pos,substr(v.fee,iter.pos,1) as c from contract_service_content v, ( select id pos from configdb ) iter where iter.pos <= length(v.fee) and ascii(substr(v.fee,iter.pos,1)) between 48 and 57 ) y group by spc_id,fee order by spc_id

執行結果:

 

 此方法參考:https://www.jianshu.com/p/289953083796

方法二:

select CAST('21歲' as SIGNED);
                                
select  CONVERT('021歲', SIGNED)

上面兩條sql 執行結果都是21

此方法明細參考 https://www.cnblogs.com/baby123/p/11716896.html 有詳細介紹

 

 

方法一  可以提取提取字串中的所有數字

方法二隻能提取到字串前面的數字,有侷限性