• 常用函数:数值函数



    数值函数

    一、MySQL数值函数

    序号

    函数

    功能

    1

    ABSx

    返回x的绝对值

    2

    CEILx

    返回大于x的最小整数值

    3

    FLOORx

    返回小于x的最大整数值

    4

    MODx,y

    返回x/y的模

    5

    RAND()

    返回0~1内的随机数

    6

    ROUNDx,y

    返回参数x的四舍五入的有y小数位的值

    7

    TRUNCATEx,y

    返回数值x截断为y小数位的结果

    1.1 ABSx

    • 求绝对值:ABSx
    mysql> select ABS(-0.5),abs(0.5);
    +-----------+----------+
    | ABS(-0.5) | ABS(0.5) |
    +-----------+----------+
    |       0.5 |      0.5 |
    +-----------+----------+
    1 row in set (0.00 sec)
    
    mysql>

    1.2 CEILx

    • 返回大于x的最小整数值:CEILx
    mysql> select ceil(-0.3),ceil(0.3);  
    +------------+-----------+
    | ceil(-0.3) | ceil(0.3) |
    +------------+-----------+
    |          0 |         1 |
    +------------+-----------+
    1 row in set (0.00 sec)
    
    mysql> 

    1.3 FLOORx

    • 返回小于x的最大整数值:FLOORx
    mysql> select floor(-0.3),floor(0.3);
    +-------------+------------+
    | floor(-0.3) | floor(0.3) |
    +-------------+------------+
    |          -1 |          0 |
    +-------------+------------+
    1 row in set (0.00 sec)
    
    mysql> 

    1.4 MODx,y

    • 返回x/y的模:MODx,y

    注意:模数和被模数任何一个为null,结果都是null

    mysql> select mod(1,2),mod(2,1),mod(null,3),mod(3,null);    
    +----------+----------+-------------+-------------+
    | mod(1,2) | mod(2,1) | mod(null,3) | mod(3,null) |
    +----------+----------+-------------+-------------+
    |        1 |        0 |        NULL |        NULL |
    +----------+----------+-------------+-------------+
    1 row in set (0.00 sec)
    
    mysql> 

    1.5 RAND()

    • 返回0~1内的随机数:RAND()

    注意:每次执行结果都不一样

    mysql> select rand(),rand();
    +--------------------+--------------------+
    | rand()             | rand()             |
    +--------------------+--------------------+
    | 0.6283992907296859 | 0.5261678123159035 |
    +--------------------+--------------------+
    1 row in set (0.00 sec)
    
    mysql> 

    1.6 ROUNDx,y

    • 返回参数x的四舍五入的有y小数位的值:ROUNDx,y

    注意,如果不写y,默认y0

    mysql> select round(0.12345,3),round(0.12345,4),round(0.12345);
    +------------------+------------------+----------------+
    | round(0.12345,3) | round(0.12345,4) | round(0.12345) |
    +------------------+------------------+----------------+
    |            0.123 |           0.1235 |              0 |
    +------------------+------------------+----------------+
    1 row in set (0.00 sec)
    
    mysql> 

    1.7 TRUNCATEx,y

    • 返回数值x截断为y小数位的结果:TRUNCATEx,y

    注意;仅仅是截断,而不是四舍五入

    mysql> select truncate(0.12345,3),truncate(0.12345,4);
    +---------------------+---------------------+
    | truncate(0.12345,3) | truncate(0.12345,4) |
    +---------------------+---------------------+
    |               0.123 |              0.1234 |
    +---------------------+---------------------+
    1 row in set (0.00 sec)
    
    mysql> 

    水果大佬
  • 相关阅读:
    《数据结构与算法Python语言描述》习题第二章第三题(python版)
    mysql中的视图
    mysql中列的增删改
    php隐藏WEBSHELL技巧
    php webshell常见函数
    MySQL join 用法
    BurpSuite 设置Hostname Resolution
    Linux mint 18.1 / Ubuntu 16.04 安装steam
    Linux SCIM/fcitx/ibus 输入法
    mysql 复制表结构 / 从结果中导入数据到新表
  • 原文地址:https://www.cnblogs.com/tanshouke/p/12360049.html
Copyright © 2020-2023  润新知