MySQL 函数的使用

目录

函数

时间日期函数:

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

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

  • 获得年月日:
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                              | 
    +-----------------------------------------+

字符串函数

数学函数

绝对值

select abs(-100.2);

向上取整

select ceiling(23.04);

向下取整

select floor(23.7);

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

select format(12.3456, 2);

产生随机数

select rand();

其他函数

  • user() 查询当前用户
select user();
  • md5(str)对一个字符串进行md5摘要,摘要后得到一个32位字符串
select current_time(); 
    +----------------+
    | current_time() | 
    +----------------+
    | 13:51:21       | 
    +----------------+
0
  • database()显示当前正在使用的数据库
select current_time(); 
    +----------------+
    | current_time() | 
    +----------------+
    | 13:51:21       | 
    +----------------+
1
  • password()函数,MySQL数据库使用该函数对用户加密
select current_time(); 
    +----------------+
    | current_time() | 
    +----------------+
    | 13:51:21       | 
    +----------------+
2
  • ifnull(val1, val2) 如果val1为null,返回val2,否则返回val1的值
select current_time(); 
    +----------------+
    | current_time() | 
    +----------------+
    | 13:51:21       | 
    +----------------+
3