mysql—將字符型數字轉成數值型數字


今天寫sql語句時,相對字符串類型的數字進行排序,怎么做呢?

需要先轉換成數字再進行排序

1.直接用加法

  字符串+0

eg:

  select * from orders order by (mark+0)  desc

2.使用函數

CAST(value as type);

CONVERT(value, type);

注:

  這里的type可以為:

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

eg:

  select * from orders order by CONVERT(mark,SIGNED)  desc

  select * from orders order by CAST(mark as SIGNED)  desc


免責聲明!

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



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