目录
-
- [1. 函数概述](#1. 函数概述)
- [2. 日期函数](#2. 日期函数)
-
- [2.1 常用日期函数](#2.1 常用日期函数)
- [2.2 基础示例](#2.2 基础示例)
- [2.3 案例-1:记录生日](#2.3 案例-1:记录生日)
- [2.4 案例-2:留言表](#2.4 案例-2:留言表)
- [3. 字符串函数](#3. 字符串函数)
-
- [3.1 常用字符串函数](#3.1 常用字符串函数)
- [3.2 基础案例](#3.2 基础案例)
- [4. 数学函数](#4. 数学函数)
-
- [4.1 常用数学函数](#4.1 常用数学函数)
- [4.2 基础示例](#4.2 基础示例)
- [5. 其它函数](#5. 其它函数)
-
- [5.1 常用其它函数](#5.1 常用其它函数)
- [5.2 基础示例](#5.2 基础示例)
- [6. 实战 OJ](#6. 实战 OJ)
在 MySQL 中,内置函数是提升 SQL 编写效率的利器。无论是日期处理、字符串拼接、数学计算,还是用户信息与 NULL 处理,合理使用函数都能让查询更简洁、更强大。本文从实战角度出发,系统梳理日期函数、字符串函数、数学函数、其它函数,并配合大量案例与执行结果,助你彻底掌握 MySQL 内置函数!
1. 函数概述
MySQL 内置函数大致可以分为以下几类:
| 分类 | 作用 |
|---|---|
| 日期函数 | 获取当前时间、日期加减、计算日期差等 |
| 字符串函数 | 拼接、截取、替换、大小写转换、长度统计等 |
| 数学函数 | 绝对值、取整、格式化、随机数、进制转换等 |
| 其它函数 | 用户信息、加密、数据库信息、NULL 处理等 |
下面逐类展开。
2. 日期函数
2.1 常用日期函数
| 函数名称 | 描述 |
|---|---|
current_date() |
当前日期 |
current_time() |
当前时间 |
current_timestamp() |
当前时间戳 |
date(datetime) |
返回 datetime 参数的日期部分 |
date_add(date, interval d_value_type) |
在 date 中添加日期或时间,单位可以是 year、minute、second、day 等 |
date_sub(date, interval d_value_type) |
在 date 中减去日期或时间 |
datediff(date1, date2) |
两个日期的差,单位是天 |
now() |
当前日期时间 |
2.2 基础示例
获得年月日:
sql
select current_date();
text
+----------------+
| current_date() |
+----------------+
| 2017-11-19 |
+----------------+
获得时分秒:
sql
select current_time();
text
+----------------+
| current_time() |
+----------------+
| 13:51:21 |
+----------------+
获得时间戳:
sql
select current_timestamp();
text
+---------------------+
| current_timestamp() |
+---------------------+
| 2017-11-19 13:51:48 |
+---------------------+
在日期的基础上加日期:
sql
select date_add('2017-10-28', interval 10 day);
text
+---------------------------------------+
| date_add('2017-10-28', interval 10 day) |
+---------------------------------------+
| 2017-11-07 |
+---------------------------------------+
在日期的基础上减去时间:
sql
select date_sub('2017-10-01', interval 2 day);
text
+--------------------------------------+
| date_sub('2017-10-01', interval 2 day) |
+--------------------------------------+
| 2017-09-29 |
+--------------------------------------+
计算两个日期之间相差多少天:
sql
select datediff('2017-10-10', '2016-09-01');
text
+--------------------------------------+
| datediff('2017-10-10', '2016-09-01') |
+--------------------------------------+
| 404 |
+--------------------------------------+
2.3 案例-1:记录生日
创建一张表,记录生日:
sql
create table tmp(
id int primary key auto_increment,
birthday date
);
添加当前日期:
sql
insert into tmp(birthday) values(current_date());
查看数据:
sql
select * from tmp;
text
+----+------------+
| id | birthday |
+----+------------+
| 1 | 2017-11-19 |
+----+------------+
2.4 案例-2:留言表
创建一个留言表:
sql
create table msg (
id int primary key auto_increment,
content varchar(30) not null,
sendtime datetime
);
插入数据:
sql
insert into msg(content, sendtime) values('hello1', now());
insert into msg(content, sendtime) values('hello2', now());
查看数据:
sql
select * from msg;
text
+----+---------+---------------------+
| id | content | sendtime |
+----+---------+---------------------+
| 1 | hello1 | 2017-11-19 14:12:20 |
| 2 | hello2 | 2017-11-19 14:13:21 |
+----+---------+---------------------+
要求 1:显示所有留言信息,发布日期只显示日期,不用显示时间。
sql
select content, date(sendtime) from msg;
要求 2:查询在 2 分钟内发布的帖子。
sql
select * from msg where date_add(sendtime, interval 2 minute) > now();
理解:
text
初始时间 now() 初始时间+2min
|------------|----------------|
只要 初始时间 + 2 分钟 仍然大于当前时间,说明发布距今不超过 2 分钟。
3. 字符串函数
3.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 中用 replace_str 替换 search_str |
strcmp(string1, string2) |
逐字符比较两字符串大小 |
substring(str, position [, length]) |
从 str 的 position 开始,取 length 个字符 |
ltrim(string)、rtrim(string)、trim(string) |
去除前空格、后空格或前后空格 |
3.2 基础案例
获取 emp 表的 ename 列的字符集:
sql
select charset(ename) from emp;
要求显示 exam_result 表中的信息,显示格式:"XXX的语文是XXX分,数学XXX分,英语XXX分":
sql
select concat(name, '的语文是', chinese, '分,数学是', math, '分,英语是', english, '分') as '分数'
from exam_result;
求学生表中学生姓名占用的字节数:
sql
select length(name), name from student;
注意:
length函数返回字符串长度,以字节为单位。如果是多字节字符则计算多个字节数;如果是单字节字符则算作一个字节。比如:字母、数字算作一个字节,中文表示多个字节数(与字符集编码有关)。
将 emp 表中所有名字中有 s 的替换成 '上海':
sql
select replace(ename, 's', '上海'), ename from emp;
截取 emp 表中 ename 字段的第二个到第三个字符:
sql
select substring(ename, 2, 2), ename from emp;
以首字母小写的方式显示所有员工的姓名:
sql
select concat(lcase(substring(ename, 1, 1)), substring(ename, 2)) from emp;
4. 数学函数
4.1 常用数学函数
| 函数名称 | 描述 |
|---|---|
abs(number) |
绝对值函数 |
bin(decimal_number) |
十进制转换二进制 |
hex(decimal_number) |
转换成十六进制 |
conv(number, from_base, to_base) |
进制转换 |
ceiling(number) |
向上去整 |
floor(number) |
向下去整 |
format(number, decimal_places) |
格式化,保留小数位数 |
rand() |
返回随机浮点数,范围 [0.0, 1.0) |
mod(number, denominator) |
取模,求余 |
4.2 基础示例
绝对值:
sql
select abs(-100.2);
向上取整:
sql
select ceiling(23.04);
向下取整:
sql
select floor(23.7);
保留 2 位小数(小数四舍五入):
sql
select format(12.3456, 2);
产生随机数:
sql
select rand();
取模:
sql
select mod(10, 3);
5. 其它函数
5.1 常用其它函数
| 函数名称 | 描述 |
|---|---|
user() |
查询当前用户 |
md5(str) |
对字符串进行 MD5 摘要,得到一个 32 位字符串 |
database() |
显示当前正在使用的数据库 |
password() |
MySQL 数据库使用该函数对用户加密 |
ifnull(val1, val2) |
如果 val1 为 NULL,返回 val2,否则返回 val1 的值 |
5.2 基础示例
查询当前用户:
sql
select user();
对字符串进行 MD5 摘要:
sql
select md5('admin');
text
+----------------------------------+
| md5('admin') |
+----------------------------------+
| 21232f297a57a5a743894a0e4a801fc3 |
+----------------------------------+
显示当前正在使用的数据库:
sql
select database();
对用户加密:
sql
select password('root');
text
+-------------------------------------------+
| password('root') |
+-------------------------------------------+
| 81F5E21E35407D884A6CD4A731AE8FB6AF209E1B |
+-------------------------------------------+
注意:MySQL 8.0 已移除
password()函数,实际开发中请使用更安全的加密方式。
ifnull 示例:
sql
select ifnull('abc', '123');
text
+----------------------+
| ifnull('abc', '123') |
+----------------------+
| abc |
+----------------------+
sql
select ifnull(null, '123');
text
+---------------------+
| ifnull(null, '123') |
+---------------------+
| 123 |
+---------------------+
6. 实战 OJ
牛客:查找字符串 '10,A,B' 中逗号 , 出现的次数 cnt。
思路:原字符串长度减去替换掉逗号后的字符串长度,差值就是逗号个数。
sql
select length('10,A,B') - length(replace('10,A,B', ',', '')) as cnt;
结果:
text
+-----+
| cnt |
+-----+
| 2 |
+-----+