MySQL 函数的使用

函数

时间日期函数:

image-20240920185414312

所有的时间日期函数都是从完整的时间日期开始,根据需求进行截断;

例如需要时间,则只显示时间部分;需要日期就显示日期部分;

  • 获得年月日:
select current_date(); 
    +----------------+
    | current_date() | 
    +----------------+
    | 2017-11-19     | 
    +----------------+
  • 获得时分秒:
select current_time(); 
    +----------------+
    | current_time() | 
    +----------------+
    | 13:51:21       | 
    +----------------+
  • 获得时间戳:
select current_timestamp(); 
    +---------------------+
    | current_timestamp() | 
    +---------------------+
    | 2017-11-19 13:51:48 | 
    +---------------------+
  • 在日期的基础上加日期:
select date_add('2017-10-28', interval 10 day); 
    +-----------------------------------------+
    | date_add('2017-10-28', interval 10 day) | 
    +-----------------------------------------+
    | 2017-11-07                              | 
    +-----------------------------------------+

字符串函数

image-20240920185611045

数学函数

image-20240920185641766

绝对值

select abs(-100.2);

向上取整

select ceiling(23.04);

向下取整

select floor(23.7);

保留2位小数位数(小数四舍五入)

select format(12.3456, 2);

产生随机

select rand();

其他函数

select user();
select md5('admin')

+----------------------------------+
| md5('admin')                     | 
+----------------------------------+
| 21232f297a57a5a743894a0e4a801fc3 | 
+----------------------------------+
select database();
select password('root'); 
    +-------------------------------------------+
    | password('root')                          | 
    +-------------------------------------------+
    | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B | 
    +-------------------------------------------+
  • ifnull(val1, val2) 如果val1为null,返回val2,否则返回val1的值
select ifnull('abc', '123'); 
    +----------------------+
    | ifnull('abc', '123') | 
    +----------------------+
    | abc                  | 
    +----------------------+
    1 row in set (0.01 sec) 
    
select ifnull(null, '123');
    +---------------------+
    | ifnull(null, '123') | 
    +---------------------+
    | 123                 | 
    +---------------------+
    1 row in set (0.00 sec)

相关文章

目录MySQL卸载环境查看是否已安装MySQL卸载mysql服务查看是否...
目录数据类型数据类型分类数值类型以TINYINT认识整型族有符号...
目录表的约束空属性非空约束(NOT NULL Constraint)默认值定...
目录函数时间日期函数:字符串函数数学函数其他函数 函数 时间...
目录使用C语言连接库的安装C APImysql_initmysql_real_conne...
目录用户用户管理查询所有用户查看当前用户查看当前连接数创...