目录
日期时间类函数
字符串函数
数学函数
其他函数
日期时间类函数
这类函数用于获取服务器当前的日期、时间或时间戳。
| 函数 | 返回值 | 示例 (假设当前时间是 2026-06-30 15:30:00) |
|---|---|---|
CURRENT_DATE() | 当前日期,格式为YYYY-MM-DD | 2026-06-30 |
CURRENT_TIME() | 当前时间,格式为HH:MM:SS | 15:30:00 |
CURRENT_TIMESTAMP()或NOW() | 当前日期和时间,格式为YYYY-MM-DD HH:MM:SS | 2026-06-30 15:30:00 |
DATE(datetime) | 返回参数的年月日的部分 | DATE(2026-06-30 15:30:00),返回2026-06-30;DATE(NOW())同理 |
| date_add(date,interval d_value_type) | 返回 date 加上 d_value_type 后的日期 | date_add(' ),返回 2026-07-10 15:30:00,date_add(now(), |
| date_sub(date,interval d_value_type) | 返回 date 减去 d_value_type 后的日期 | 类比 date_add |
| datediff(date1,date2) | 返回 date1 - date2 ,单位是 day | datediff('2017-10-10', '2016-9-1') 返回 404,datediff(date(now()),2016-9-1)同理 |
案例:生日表
// 创建一张表,记录生日 create table tmp( id int primary key auto_increment, birthday date ); // 注意 date 是 date 类型的 // 添加当前日期: insert into tmp(birthday) values(current_date()); insert into tmp(birthday) values(current_time()); mysql> select * from tmp; +----+------------+ | id | birthday | +----+------------+ | 1 | 2017-11-19 | +----+------------+ | 2 | 2017-11-19 | +----+------------+从上面的案例我们注意到:current_time() 返回的是时分秒,居然可以正确赋值给 date 类型的 birthday。
案例:获取两分钟以内的评论
// 创建一个留言表 mysql> create table msg ( id int primary key auto_increment, content varchar(30) not null, sendtime datetime ); // 插入数据 mysql> insert into msg (content,sendtime) values('纸上得来终觉浅', now()); mysql> insert into msg (content,sendtime) values('恐惊天上人', now()); // 获取两分钟以内的评论 mysql> select content,sendtime from msg mysql> where datediff(now(),interval 2 minute) < sendtime;字符串函数
| 函数 | 返回值 | 示例 |
|---|---|---|
| charset('str') | 返回str的编码集 | charset('abc'),返回 utf8 select charset(column_name) form table_name,返回表中某一列的编码集 |
| concat('str1','str2','str3',...) | 返回拼接后的字符串 | concat('a','b','c') 返回 abc concat('abc','bef'),返回abcdef |
| instr('str1','str2') | 返回 str2 在 str1 出现的位置,没有就返回 0 | instr('abc','b')返回 1 |
| ucase('str') | 将str转换为大写 | ucase('abc123')返回 ABC123 |
| lcase('str') | 将str转换为小写 | ucase('ABC123')返回 abc123 |
| left('str',len) 和 right('str',len) | left从str开始提取 len 个字符,right从str结尾提取 len 个字符, | left('abc1234',4)返回 abc1,right('abc1234',4) 返回 1234 |
| length('str‘) | 返回str的长度,单位是字节 | length('abc') 返回 3, length('中国') 返回 6 |
| replace('str','aim_str','replace_str') | 在 str 中找到 aim_str,并将它替换为 replace_str | replace('abcXXX1234','XXX',def),' abcXXX1234变成abcdef1234 |
| strcmp('str1','str2') | 比较 str1 和 str2 的字典序大小,如果 str1 = str2 返回 0,如果 str1 > str2 返回 1,如果 str1 < str2,返回 -1 | strcmp('abc','abc') 返回 0 strcmp('bbc','abc') 返回 1 strcmp('abc','bbc') 返回 -1 |
| substring('str',pos,len) | 从 str 的 pos 位置开始截取 len 长度的 字符串.len 可以省略,表示截取到末尾 | substring('abcdef',4,3) 返回 def substring('abcdef',2) 返回bcdef |
| ltrim('str')、rtrim('str')、trim('str') | trim:意为修剪,ltrim:将str左边的空格去掉,rtrim:将str右边的空格去掉,trim:将str左边和右边的空格都去掉 | ltrim(' abc ')返回’abc ‘ rtrim(' abc ')返回’ abc‘ trim(' abc ')返回’abc‘ |
数学函数
| 函数 | 说明 | 示例 |
|---|---|---|
ABS(x) | 返回x的绝对值 | ABS(-5.6)→5.6 |
| bin(x) | 十进制转二进制 | bin(10) → 1010 |
| hex(x) | 十进制转十六进制 | bin(11) → B |
| conv(x,进制1,进制2) | 将 x 从进制1转换为进制2 | conv(10,10,2) → 1010 |
MOD(x, y)或x % y | 返回x除以y的余数(取模) | MOD(10, 3)→110 % 3→1 |
CEIL(x)或CEILING(x) | 返回不小于x的最小整数(向上取整) | CEIL(3.14)→4CEIL(-3.14)→-3 |
FLOOR(x) | 返回不大于x的最大整数(向下取整) | FLOOR(3.14)→3FLOOR(-3.14)→-4 |
| format(x,y) | 保留 x 的 y 位小数(不做四舍五入) | format(3.1415926,4) → 3.1415 |
ROUND(x, d) | 将x四舍五入到d位小数;若d省略则取整为整数 | ROUND(3.14159, 2)→3.14ROUND(3.5)→4 |
TRUNCATE(x, d) | 将x截断到d位小数(不四舍五入) | TRUNCATE(3.14159, 2)→3.14 |
| rand() | 返回随机浮点数,范围 [0.0,1.0) | 0.17174184338351883 |
其他函数
| 函数 | 说明 | 示例 |
|---|---|---|
| user() | 查询当前用户 | \ |
| database() | 查询当前在哪个数据库 | bin(10) → 1010 |
| md5('str') | 对一个字符串进行md5摘要,摘要后得到一个32位字符串,通常用于数据库对用户密码进行加密 | md5('1234') → 81dc9bdb52d04dc20036db... |
| password('str') | 专门用于数据库对用户密码进行加密 | password('1234') → *A4B6157319038724E3560.... |
| ifnull('str1','str2') | 如果 str1 为 null,返回 str2,否则返回 str1 | ifnull('abc','123') →abc ifnull('abc',null) →abc ifnull(null,'123') →123 ifnull(null,null) →null |