🏝️专栏:Mysql_猫咪-9527的博客-CSDN博客
🌅主页:猫咪-9527-CSDN博客“欲穷千里目,更上一层楼。会当凌绝顶,一览众山小。”
目录
7.函数
7.1 日期函数
函数总:编辑
获得当前日期
获得当前时间
获得时间戳
在日期的基础上加日期
在日期的基础上减去日期
计算两个日期之间相差多少天
案例1:
案例二:
7.2 字符串函数
函数总:
获取表中列的字符集
格式化字符串
查找字符串所出现的位置
计算字符串字节长度
替换字符串中的字符
截取字符串的一部分
转换大小写
删除空格
7.3 数学函数
绝对值
向上取整
向下取整
四舍五入保留小数位
产生随机数
7.4 其它函数
查询当前用户
MD5 摘要
显示当前数据库
密码加密
判断是否为 NULL
7.函数
7.1 日期函数
函数总:
-
获得当前日期
select current_date();返回当前日期,格式为YYYY-MM-DD。
select current_date();
-
获得当前时间
select current_time();返回当前时间,格式为HH:MM:SS。
select current_time();
select current_date();select current_time();
-
获得时间戳
select current_timestamp();返回当前的日期和时间,格式为YYYY-MM-DD HH:MM:SS。
select current_timestamp();
select now();
select current_timestamp();select now();
-
在日期的基础上加日期
select date_add('2017-10-28', interval 10 day);将日期2017-10-28加上 10 天,返回结果为2017-11-07。
select date_add('2025-3-24',interval 17 day);
-
在日期的基础上减去日期
select date_sub('2017-10-1', interval 2 day);将日期2017-10-01减去 2 天,返回结果为2017-09-29。
select date_sub(now(),interval 10 day);
select date_add('2025-3-24',interval 17 day);select date_sub(now(),interval 10 day);
-
计算两个日期之间相差多少天
select datediff('2017-10-10', '2016-9-1');计算两个日期之间的差值,返回结果为404天。
select datediff('2025-2-24',now());

案例1:
创建一个生日表
create table birthday(
id int primary key auto_increment,
birthday date);
添加当前时间为生日:
insert birthday(birthday) values(current_date());

案例二:
创建一个评论表:
create table comments(
id int primary key auto_increment,
commtent varchar(200),
release_time datetime
);
插入评论:
insert comments(commtent,release_time) values('千金散尽还复来',now());
查找两分钟之前的评论:

7.2 字符串函数
函数总:

-
获取表中列的字符集
select charset(ename) from EMP;返回ename列的字符集。
select charset(ename) from emp;

-
格式化字符串
select concat(name, '的语文是', chinese, '分,数学是', math, '分') as '分数' from exam_result;将学生的成绩按照指定格式输出。
select concat(name,'的数学成绩是:',math,'的语文成绩是:',
chinese,',英语成绩是:',english) from exam_result;

-
查找字符串所出现的位置
- select instr(string,substring);查看substring在string中所出现的位置,成功返回第几个字符,失败返回0
| |
![]() | ![]() |
-
计算字符串字节长度
select length(name), name from exam_result;获取name字段的字节长度(根据字符集不同,中文可能占多个字节)。
select name,length(name) from exam_result;

注:一个汉字在utf_8中占据3个字节
-
替换字符串中的字符
select replace(ename, 'S', '上海') ,ename from EMP;将ename字段中的S替换为上海。
select replace(job,'S','上海')from emp;

-
截取字符串的一部分
select substring(ename, 2, 2), ename from EMP;截取ename字段从第二个字符开始的两个字符。
select substring('string',2,2);

-
转换大小写
- ucase(string),将string全部变为大写
- lcase(string),将string全部变为小写
| |
![]() | ![]() |
-
删除空格
- ltrim(string) 删除string左边的空格
- rtrim(string)删除string右边的空格
- trim(string)删除string左右两边的空格
| |
![]() | ![]() |
| |
![]() | ![]() |
7.3 数学函数

-
绝对值
select abs(-100.2);返回 100.2,表示绝对值。
| | |
![]() | ![]() | ![]() |
-
向上取整
select ceiling(23.04);返回 24,表示向上取整。
| | |
![]() | ![]() | ![]() |
-
向下取整
select floor(23.7);返回 23,表示向下取整。
| | |
![]() | ![]() | ![]() |
-
四舍五入保留小数位
select format(12.3456, 2);返回12.35,保留 2 位小数。
| | |
![]() | ![]() | ![]() |
-
产生随机数
select rand();返回一个 0 到 1 之间的随机浮动数值。
select rand();

生成0到99的随机数
![]()
7.4 其它函数
-
查询当前用户
select user();返回当前数据库用户的信息。
-
MD5 摘要
select md5('admin');对字符串'admin'进行 MD5 加密,返回加密后的结果。
-
显示当前数据库
select database();返回当前正在使用的数据库名称。
-
密码加密
select password('root');对'root'进行加密,返回加密后的结果。
-
判断是否为 NULL
select ifnull('abc', '123');如果第一个参数为NULL,则返回第二个参数。否则返回第一个参数。

























