《MySQL》内置函数

文章目录

  • 日期函数
  • 字符串函数
  • 数学函数
  • 其他函数

日期函数

函数名称 功能
current_date() 当前日期(yyyy-mm-dd)
current_time() 当前时间(hh-mm-ss)
current_timestamp() 当前时间戳(yyyy-mm-dd hh-mm-ss)
now() 当前日期时间
date(datetime) 返回datetime参数的日期部分
date_add(date, interval d_value_type) 返回date加上d的日期时间,d可以是天、时、分、秒
date_sub(date, interval d_value_type) 返回date减上d的日期时间,d可以是天、时、分、秒
datediff(date1, date2) date1 - date2的日期差,单位是天

在这里插入图片描述

《MySQL》内置函数_第1张图片

字符串函数

函数名称 功能
charset(str) 返回字符串字符集
concat(string2, [, …] 连接字符串
instr(string, substring) 返回substring在string中出现的位置,没有返回0
ucase(string2) 转换成大写
lcase(string2) 转换成小写
left(string2, length) 从string2中的左边起取length个字符
length(string) string的长度
replace(str, search_str, replace_str) 把str中的search_str替换成replace_str
strcmp(str1, str2) 逐字符比较两个字符串大小
substring(str, position, length) 从str的postion开始,取length个字符
ltrim(str) / rtrim(str) / trim(str) 去除左/右/两边的空格
  • charset() 和 concat()
    《MySQL》内置函数_第2张图片
  • instr() 和 ucase() 和 lcase()
    《MySQL》内置函数_第3张图片
  • left() 和 right() 和 length()
    《MySQL》内置函数_第4张图片
  • replace() 和 substring()
    《MySQL》内置函数_第5张图片
  • ltrim() 和 trim() 和 rtrim()
    在这里插入图片描述

数学函数

函数名称 功能
abs() 绝对值函数
bin(num) 十进制转二进制
hex(num) 十进制转十六进制
conv(num, from_base, to_base) 进制转换
ceiling(num) 向上取整
floor(num) 向下取整
format(num, decimal_places) 格式化,保留小数位数
rand() 随机数,[0.0,1.0)
mod(num, denominator) 取模

其他函数

函数名称 功能
ifnull(val1, val2) 如果val1为null,返回val2,否则返回val1的值
password() MySQL数据库使用该函数对用户加密
database() 显示当前正在使用的数据库
md5(str) 对一个字符串进行md5摘要,摘要后得到一个32位字符串
user() 查询当前用户

你可能感兴趣的:(MySQL,mysql,数据库)