文章目录
mysql内置函数
本篇文章,我们将重点对mysql内的一些内置函数进行理解和学习!
日期函数
我们学习过mysql数据类型,我们知道mysql中对于日期是有专门的类型的!
由此可见:在mysql存储数据中,日期是非常重要的!
所以,为了方便用户的操作,mysql提供了一些关于日期的内置函数:

日期相关信息获取
下面我们一起来尝试使用一下:
sql
# 获取当前日期 -> 年月日
mysql> select current_date();
+----------------+
| current_date() |
+----------------+
| 2025-11-07 |
+----------------+
1 row in set (0.00 sec)
# 获取当前时间 -> 时分秒
mysql> select current_time();
+----------------+
| current_time() |
+----------------+
| 09:30:58 |
+----------------+
1 row in set (0.00 sec)
# 获取当前时间戳 -> 自1970 1月1日 中时区0时 到现在经过的秒数 -> 以 日期 + 时间 显示
mysql> select current_timestamp();
+---------------------+
| current_timestamp() |
+---------------------+
| 2025-11-07 09:31:11 |
+---------------------+
1 row in set (0.00 sec)
# 获取当前日期时间 -> 以 日期 + 时间 显示
mysql> select now();
+---------------------+
| now() |
+---------------------+
| 2025-11-07 09:35:58 |
+---------------------+
1 row in set (0.00 sec)
# 获取日期中间中的日期 -> 细节:数字可以省略前置0,但是必须要以字符串的形式传入
mysql> select date('2025-11-7 9:30:2');
+--------------------------+
| date('2025-11-7 9:30:2') |
+--------------------------+
| 2025-11-07 |
+--------------------------+
1 row in set (0.00 sec)
对于mysql内的函数来说:也是可以嵌套使用的:
sql
# 嵌套使用函数 获取当前日期
mysql> select date(now());
+-------------+
| date(now()) |
+-------------+
| 2025-11-07 |
+-------------+
日期的加减
早在学习c++类相关内容的时候,就已经简单实现过一个日期类!这里就不再介绍日期的加减的规则了,我们直接来看结果!
date_add:
sql
# 以当前日期时间为起点 计算100天后日期时间
mysql> select date_add(now(), interval 100 day);
+-----------------------------------+
| date_add(now(), interval 100 day) |
+-----------------------------------+
| 2026-02-15 09:42:50 |
+-----------------------------------+
1 row in set (0.00 sec)
# 以当前日期时间为起点 计算100分钟后日期时间
mysql> select date_add(now(), interval 100 minute);
+--------------------------------------+
| date_add(now(), interval 100 minute) |
+--------------------------------------+
| 2025-11-07 11:22:55 |
+--------------------------------------+
1 row in set (0.00 sec)
# 以日期 2025-10-01为起点 计算1天后的日期
mysql> select date_add('2025-10-01', interval 1 day);
+----------------------------------------+
| date_add('2025-10-01', interval 1 day) |
+----------------------------------------+
| 2025-10-02 |
+----------------------------------------+
1 row in set (0.00 sec)
date_sub:
sql
# 用法和date_add是很类似的 这里就不做过多演示了
mysql> select date_sub(date(now()), interval 10 day);
+----------------------------------------+
| date_sub(date(now()), interval 10 day) |
+----------------------------------------+
| 2025-10-28 |
+----------------------------------------+
1 row in set (0.00 sec)
日期的差值
如果说,我们想要计算某两个日期之间差多少时间,我们可以使用datediff函数:
sql
# 计算新中国成立至今经过的天数
mysql> select datediff('1949-10-01 00:00:00', now());
+----------------------------------------+
| datediff('1949-10-01 00:00:00', now()) |
+----------------------------------------+
| -27796 |
+----------------------------------------+
1 row in set (0.00 sec)
mysql> select datediff('1949-10-01', date(now()));
+-------------------------------------+
| datediff('1949-10-01', date(now())) |
+-------------------------------------+
| -27796 |
+-------------------------------------+
1 row in set (0.00 sec)
对于datediff函数的使用要点:
1.计算的是日期之差!时间(时分秒)是无效的!
2.日期做差得到的是天数:如果是小的日期减去大的日期,得到的是负数!
使用案例
1.创建一个留言表

如果是想要具体的日期 + 时间:可以把send_time的类型更改为datetime!
2.找出最近三分钟内的评论
sql
mysql> select * from msg;
+-----------------------+---------------------+
| content | send_time |
+-----------------------+---------------------+
| 不知庐山真面目 | 2025-11-07 10:05:31 |
| 只缘身在此山中 | 2025-11-07 10:05:46 |
| 床前明月光 | 2025-11-07 10:08:49 |
| 会当临绝顶 | 2025-11-07 10:12:10 |
+-----------------------+---------------------+
4 rows in set (0.00 sec)
mysql> select * from msg where send_time > date_sub(now(), interval 3 minute);
+-----------------+---------------------+
| content | send_time |
+-----------------+---------------------+
| 会当临绝顶 | 2025-11-07 10:12:10 |
+-----------------+---------------------+
1 row in set (0.00 sec)
解释:
如果我们要求的是以当前时间为基准:三分钟内的评论,其实很简单:
只需要表中的时间send_time 大于 当前的时间减去3分钟即可!
又或者是send_time的时间 + 2大于当前时间!

字符串函数
我们学习过编程语言,对于字符串的操作是非常熟悉的,我们现在来看看mysql内的:

其实我们真正用起来后会发现,其实和c++内的操作是很像的!
字符串基本信息获取
我们在建表的时候,是可以指定字符集的!或者是校验规则!
我们可以通过charset查询某个字符串的编码规则:
sql
mysql> select charset('你好');
+-------------------+
| charset('你好') |
+-------------------+
| utf8mb4 |
+-------------------+
1 row in set (0.00 sec)
我的系统内设置了默认校验规则是utf8mb4,所以查出来是这个结果!
不过我们可以自己创建一个不同字符集的表(用show charset;指令查询,随便选一个)即可!
这里就不再过多展示,知道有这么个用法即可!
又或者说:我们想得到字符串的长度:
sql
mysql> select length('i am a man');
+----------------------+
| length('i am a man') |
+----------------------+
| 10 |
+----------------------+
1 row in set (0.00 sec)
拼接字符串
这个就等同于c语言内的:strcat,又或者是c++ string的 +=:
sql
mysql> select concat('i am ', 'handsome man');
+---------------------------------+
| concat('i am ', 'handsome man') |
+---------------------------------+
| i am handsome man |
+---------------------------------+
1 row in set (0.00 sec)
查找字符串或替换
我们可以在字符串中查到一个子串,类似于c++的string.find():
sql
mysql> select instr('hello world', 'hello');
+-------------------------------+
| instr('hello world', 'hello') |
+-------------------------------+
| 1 |
+-------------------------------+
1 row in set (0.00 sec)
mysql> select instr('hello world', 'wor');
+-----------------------------+
| instr('hello world', 'wor') |
+-----------------------------+
| 7 |
+-----------------------------+
1 row in set (0.00 sec)
mysql> select instr('hello world', 'worasd');
+--------------------------------+
| instr('hello world', 'worasd') |
+--------------------------------+
| 0 |
+--------------------------------+
1 row in set (0.00 sec)
这里我们要知道的是:
如果子串不存在,结果是0
存在的情况下,返回的是子串在原串的起始位置(注意:第一个字符的坐标对应的是1!)
当然,我们如果想替换掉被找到的子串也是可以的,比如把world换成Linux:
sql
mysql> select replace('hello world', 'world', 'Linux');
+------------------------------------------+
| replace('hello world', 'world', 'Linux') |
+------------------------------------------+
| hello Linux |
+------------------------------------------+
1 row in set (0.00 sec)
字符串大小写转换
有时候我们希望能够把字符串进行大小写转换。
就可以使用lcase(lower) 和 ucase(upper):
sql
mysql> select lcase('ABCD145ada');
+---------------------+
| lcase('ABCD145ada') |
+---------------------+
| abcd145ada |
+---------------------+
1 row in set (0.00 sec)
mysql> select ucase('adsasd145ad.das./a');
+-----------------------------+
| ucase('adsasd145ad.das./a') |
+-----------------------------+
| ADSASD145AD.DAS./A |
+-----------------------------+
1 row in set (0.00 sec)
注意:这里只会对字母进行转换!非字母字符不处理。
字符串的截取
在mysql的字符串的截取中,是有其自定义的一些功能的:
比如从左边 / 右边截取长度为len的字符串:
sql
mysql> select left('abcdefg', 2);
+--------------------+
| left('abcdefg', 2) |
+--------------------+
| ab |
+--------------------+
1 row in set (0.00 sec)
mysql> select right('abcdefg', 2);
+---------------------+
| right('abcdefg', 2) |
+---------------------+
| fg |
+---------------------+
1 row in set (0.00 sec)
如果截取的长度超出字符串的长度,那就截取整个字符串完成后就结束!
如果想要截取的是某一段区间内的字符串,也是可以的,类似于string.substr():
sql
mysql> select substring('hello world', 2, 3);
+--------------------------------+
| substring('hello world', 2, 3) |
+--------------------------------+
| ell |
+--------------------------------+
1 row in set (0.00 sec)
mysql> select substring('hello world', 20, 3);
+---------------------------------+
| substring('hello world', 20, 3) |
+---------------------------------+
| |
+---------------------------------+
1 row in set (0.00 sec)
mysql> select substring('hello world', 5, 100);
+----------------------------------+
| substring('hello world', 5, 100) |
+----------------------------------+
| o world |
+----------------------------------+
1 row in set (0.00 sec)
mysql内的substring函数其实就是和substr类似:
都是从某个字符串内,pos位置开始,截取长度为len的子串!
只不过要注意的是:mysql的字符串内,第一个字符的位置是1!
而且,位置越界了,不是报错,而是截取出一个空串。
字符串的大小比较
字符串的比较,就是根据字典序来进行比较:
sql
mysql> select strcmp('abcd', 'abc');
+-----------------------+
| strcmp('abcd', 'abc') |
+-----------------------+
| 1 |
+-----------------------+
1 row in set (0.00 sec)
mysql> select strcmp('abc', 'abc');
+----------------------+
| strcmp('abc', 'abc') |
+----------------------+
| 0 |
+----------------------+
1 row in set (0.00 sec)
mysql> select strcmp('abc', 'abcd');
+-----------------------+
| strcmp('abc', 'abcd') |
+-----------------------+
| -1 |
+-----------------------+
1 row in set (0.00 sec)
用法和c语言内的函数是基本一样的!
去除空格
mysql中给予用户去除字符串种空格的功能:
使用ltrim,只删除字符串左边的前置空格:
sql
mysql> select ltrim(' a abcde a');
+------------------------+
| ltrim(' a abcde a') |
+------------------------+
| a abcde a |
+------------------------+
1 row in set (0.00 sec)
使用ltrim,只删除字符串右边的后置空格:
sql
mysql> select rtrim(' a daas ');
+---------------------+
| rtrim(' a daas ') |
+---------------------+
| a daas |
+---------------------+
1 row in set (0.00 sec)
# 因为显示效果原因导致不明显 这里给多一个测试用例
mysql> select rtrim(' a daas c');
+----------------------+
| rtrim(' a daas c') |
+----------------------+
| a daas c |
+----------------------+
1 row in set (0.00 sec)
使用trim就是ltrim和rtrim的结合了,即取出首尾空格!
这里就不再展示效果了!
数学函数
数学函数:就是一些专门针对于数字操作的一些函数!

基本操作
对一个数求绝对值:
sql
mysql> system clear
mysql> select abs(10.34);
+------------+
| abs(10.34) |
+------------+
| 10.34 |
+------------+
1 row in set (0.00 sec)
mysql> select abs(-10.34);
+-------------+
| abs(-10.34) |
+-------------+
| 10.34 |
+-------------+
1 row in set (0.00 sec)
取模操作:
sql
mysql> select mod(5, 3);
+-----------+
| mod(5, 3) |
+-----------+
| 2 |
+-----------+
1 row in set (0.00 sec)
mysql> select mod(5.1, 3);
+-------------+
| mod(5.1, 3) |
+-------------+
| 2.1 |
+-------------+
1 row in set (0.00 sec)
mysql> select mod(-3, 3);
+------------+
| mod(-3, 3) |
+------------+
| 0 |
+------------+
1 row in set (0.00 sec)
mysql> select mod(-3, -2);
+-------------+
| mod(-3, -2) |
+-------------+
| -1 |
+-------------+
1 row in set (0.00 sec)
这里对负数的取模需要注意:没有强制让结果为正数!
获取一个随机数(获取到的是0~1的一个小数):
sql
mysql> select rand();
+---------------------+
| rand() |
+---------------------+
| 0.11799829277954836 |
+---------------------+
1 row in set (0.00 sec)
如果想要更大的随机数呢?我们可以把rand()的结果当成一个比例:
只需要乘以一个倍数即可!
sql
mysql> select rand() * 1000;
+-------------------+
| rand() * 1000 |
+-------------------+
| 595.0977304904701 |
+-------------------+
1 row in set (0.00 sec)
mysql> select rand() * 100;
+-------------------+
| rand() * 100 |
+-------------------+
| 62.14938048473502 |
+-------------------+
1 row in set (0.00 sec)
格式化控制
我们可以通过一个format函数,对浮点数进行格式控制:
sql
mysql> select format(rand() * 100, 2);
+-------------------------+
| format(rand() * 100, 2) |
+-------------------------+
| 45.66 |
+-------------------------+
1 row in set (0.00 sec)
# 这就解决了如何随机选取一个整数的问题了
mysql> select format(rand() * 100, 0);
+-------------------------+
| format(rand() * 100, 0) |
+-------------------------+
| 35 |
+-------------------------+
1 row in set (0.00 sec)
# 不足小数位的 用0补充
mysql> select format(100, 50);
+------------------------------------+
| format(100, 50) |
+------------------------------------+
| 100.000000000000000000000000000000 |
+------------------------------------+
1 row in set (0.00 sec)
取整函数
介绍取整函数前,需要先介绍几个取整的方式
- 四舍五入,这种是我们比较常见的一种!
- 0向取整:(取整的方向朝着0,入1.25 -> 1, -1.25 -> -1)
>3. 向上取整:朝着比自己大的方向取整
- 向下取整:朝着比自己小的方向取整
现在我们再来看内置函数:
1.round函数,四舍五入:
sql
mysql> select round(26);
+-----------+
| round(26) |
+-----------+
| 26 |
+-----------+
1 row in set (0.00 sec)
mysql> select round(26.2);
+-------------+
| round(26.2) |
+-------------+
| 26 |
+-------------+
1 row in set (0.00 sec)
mysql> select round(26.55);
+--------------+
| round(26.55) |
+--------------+
| 27 |
+--------------+
1 row in set (0.00 sec)
2.ceiling函数(向上取整)
sql
mysql> select ceiling(3.5);
+--------------+
| ceiling(3.5) |
+--------------+
| 4 |
+--------------+
1 row in set (0.00 sec)
mysql> select ceiling(-2.5);
+---------------+
| ceiling(-2.5) |
+---------------+
| -2 |
+---------------+
1 row in set (0.00 sec)
3.floor函数(向下取整)
sql
mysql> select floor(3.5);
+------------+
| floor(3.5) |
+------------+
| 3 |
+------------+
1 row in set (0.00 sec)
mysql> select floor(-2.5);
+-------------+
| floor(-2.5) |
+-------------+
| -3 |
+-------------+
1 row in set (0.00 sec)
进制转换
我们使用的是十进制,mysql提供了一些可以让我们进行进制转换的函数!
常用的:十进制转二进制,十六进制:
sql
mysql> select bin(36);
+---------+
| bin(36) |
+---------+
| 100100 |
+---------+
1 row in set (0.00 sec)
mysql> select hex(36);
+---------+
| hex(36) |
+---------+
| 24 |
+---------+
1 row in set (0.00 sec)
十进制转其他进制:
使用conv函数,把一个数从某个进制转化为另一个进制:
sql
mysql> select conv(0x2A, 16, 10);
+--------------------+
| conv(0x2A, 16, 10) |
+--------------------+
| 42 |
+--------------------+
1 row in set (0.00 sec)
mysql> select conv('2a', 16, 10);
+--------------------+
| conv('2a', 16, 10) |
+--------------------+
| 42 |
+--------------------+
1 row in set (0.00 sec)
mysql> select conv('1010', 2, 10);
+---------------------+
| conv('1010', 2, 10) |
+---------------------+
| 10 |
+---------------------+
1 row in set (0.00 sec)
mysql> select conv(200, 10, 8);
+------------------+
| conv(200, 10, 8) |
+------------------+
| 310 |
+------------------+
1 row in set (0.00 sec)
需要特别注意的是:像是10进制以上的,需要添加字母充当数字的时候:
我们需要传入的应该是字符串!
其他函数
查询当前用户(user):
sql
mysql> select user();
+----------------+
| user() |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)
但是当前我们还没有学习用户管理等相关知识,先做一个了解即可!
对数据做摘要(md5):
我们在学习HTTPS的时候,就了解过摘要这个内容!
sql
mysql> select md5('hello');
+----------------------------------+
| md5('hello') |
+----------------------------------+
| 5d41402abc4b2a76b9719d911017c592 |
+----------------------------------+
1 row in set (0.00 sec)
mysql> select md5('a');
+----------------------------------+
| md5('a') |
+----------------------------------+
| 0cc175b9c0f1b6a831c399e269772661 |
+----------------------------------+
1 row in set (0.00 sec)
摘要其实就是对字符串/文本信息,生成一个字符串序列!
其中md5函数生成的是长度为32位的字符串
特点就是,不管原串长度多少,最终的摘要都是32位!
因此:我们可以用摘要函数来判断两个文件内容是否完全相同!也可以是用来做加密
其他常见算法有:SHA1,SHA2...
判断当前正在使用的数据库(database):
sql
mysql> select database();
+------------+
| database() |
+------------+
| test |
+------------+
1 row in set (0.00 sec)
这个早在mysql的入门就介绍了,这里不再介绍!
基于null的三目运算(ifnull):
ifnull(val1, val2):判断val1是否为空,如果不是返回val1,反之val2:
sql
mysql> select ifnull(1, 'avf');
+------------------+
| ifnull(1, 'avf') |
+------------------+
| 1 |
+------------------+
1 row in set (0.00 sec)
mysql> select ifnull(null, 'avf');
+---------------------+
| ifnull(null, 'avf') |
+---------------------+
| avf |
+---------------------+
1 row in set (0.00 sec)
mysql> select ifnull('1ad', null);
+---------------------+
| ifnull('1ad', null) |
+---------------------+
| 1ad |
+---------------------+
1 row in set (0.00 sec)
>3. 向上取整:朝着比自己大的方向取整
