mysql 常用關鍵字操作(字符串轉數字,字符串截取)


mysql將字符串轉為數字

方法1 : 直接使用+0  

  栗子:select * from student order by (status+0)  desc

方法2 : CONVERT(value, type);

  栗子:select * from student order by CONVERT(status,SIGNED)  desc

方法3 : CAST(value as type);

  栗子:select * from student order by CAST(status as SIGNED)  desc

注:方法 2和3 的 type

  浮點數 : DECIMAL 
  整數 : SIGNED
  無符號整數 : UNSIGNED 

------------------------------------------------------------------------------------------------------------------------------------------------------------

mysql字符串截取 SUBSTRING_INDEX()

substring_index(str,delim,count)

      str:要處理的字符串

      delim:分隔符

      count:計數

例子:str=www.wikibt.com

      substring_index(str,'.',1)

      結果是:www

      substring_index(str,'.',2)

      結果是:www.wikibt

      也就是說,如果count是正數,那么就是從左往右數,第N個分隔符的左邊的全部內容

      相反,如果是負數,那么就是從右邊開始數,第N個分隔符右邊的所有內容,如:

      substring_index(str,'.',-2)

      結果為:wikibt.com

     有人會問,如果我要中間的的wikibt怎么辦?

     很簡單的,兩個方向:

      從右數第二個分隔符的右邊全部,再從左數的第一個分隔符的左邊:

  substring_index(substring_index(str,'.',-2),'.',1);

轉自:https://www.cnblogs.com/mqxs/p/7380933.html

------------------------------------------------------------------------------------------------------------------------------------------------------------

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM