• mysql将字符串转成数字


    今天写sql语句时,相对字符串类型的数字进行排序,怎么做呢?

    需要先转换成数字再进行排序

    1.直接用加法

      字符串+0

    eg:

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

    eg:

    以分类字段进行分组,获取分类总数amount,和qty(数量),最后以amount进行有大到小的倒序排列

    SELECT category_code,SUM(transaction_number) as qty ,FORMAT(SUM(number*price),2)+0 as amount FROM sell WHERE date='2017-03-06' GROUP BY category_code ORDER BY amount 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

    踩过这个坑,还有下一个坑等着你,这一路就是给自己填坑,坑填多了,也就习惯了,直到这一路平坦了,也就无怨无悔了。
  • 相关阅读:
    编译安装linux内核步骤
    怎样在github上协同开发
    在JSP里使用CKEditor和CKFinder
    tomcat结合nginx使用小结
    JAVA学习笔记——(五)
    JAVA学习笔记——(四)
    JAVA学习笔记——(三)
    JAVA学习笔记——(二)
    JAVA学习笔记——(一)
    ansible
  • 原文地址:https://www.cnblogs.com/xiaofeilin/p/14060367.html
Copyright © 2020-2023  润新知