【MySQL】内置函数

一、日期函数

函数名称描述
current_date()当前日期
current_time()当前时间
date(datetime)返回datetime参数的日期部分
date_add(date, interval d_value_type)在date中添加日期或时间,interval后的数值单位可以是:year minute second day
date_sub(date, interval d_value_type)在date中减去日期或时间,interval后的数值单位可以是:year minute second day
current_timestamp()当前时间戳
datediff(date2, date2)两个日期的差,单位是天
now()当前日期时间

1.1 current_date函数

获得年月日:

select current_date();
+----------------+
| current_date() |
+----------------+
| 2024-12-03 	 |
+----------------+

1.2 current_time函数

获得时分秒:

select current_time();
+----------------+
| current_time() |
+----------------+
| 18:49:32 		 |
+----------------+

1.3 current_timestamp函数

获得时间戳:

mysql> select current_timestamp();
+---------------------+
| current_timestamp() |
+---------------------+
| 2024-12-03 18:49:32 |
+---------------------+
1 row in set (0.00 sec)

1.4 date_add函数

在日期的基础上加日期:

smysql> select date_add('2024-12-03', interval 10 day);
+-----------------------------------------+
| date_add('2024-12-03', interval 10 day) |
+-----------------------------------------+
| 2024-12-13                              |
+-----------------------------------------+
1 row in set (0.00 sec)

1.5 date_sub函数

在日期的基础上减去时间:

mysql> select date_sub('2024-12-03', interval 2 day);
+----------------------------------------+
| date_sub('2024-12-03', interval 2 day) |
+----------------------------------------+
| 2024-12-01                             |
+----------------------------------------+
1 row in set (0.00 sec)

1.6 datediff函数

计算两个日期之间相差多少天:

mysql> select datediff('2024-10-10', '2023-9-1');
+------------------------------------+
| datediff('2024-10-10', '2023-9-1') |
+------------------------------------+
|                                405 |
+------------------------------------+
1 row in set (0.00 sec)

1.7 now函数

获取当前日期时间

mysql> select now();
+---------------------+
| now()               |
+---------------------+
| 2024-12-03 19:07:04 |
+---------------------+
1 row in set (0.00 sec)

综合使用1

创建一张表,记录生日

create table tmp(
id int primary key auto_increment,
birthday date
);

添加当前日期

insert into tmp(birthday) values(current_date());

结果:

mysql> select * from tmp;
+----+------------+
| id | birthday   |
+----+------------+
|  1 | 2024-12-03 |
+----+------------+
1 row in set (0.00 sec)

综合使用2

创建一个留言表

create table msg (
id int primary key auto_increment,
content varchar(30) not null,
sendtime datetime
);

插入数据

insert into msg(content,sendtime) values('hello1', now());
insert into msg(content,sendtime) values('hello2', now());

结果:

mysql> select * from msg;
+----+---------+---------------------+
| id | content | sendtime            |
+----+---------+---------------------+
|  1 | hello1  | 2024-12-03 18:57:48 |
|  2 | hello2  | 2024-12-03 18:57:58 |
+----+---------+---------------------+
2 rows in set (0.00 sec)

显示所有留言信息,发布日期只显示日期,不用显示时间

select content,date(sendtime) from msg;

请查询在2分钟内发布的帖子

select * from msg where date_add(sendtime, interval 2 minute) > now();

二、字符串函数

函数名称函数说明
charset(str)返回字符串字符集
concat(string[, …])连接字符串
instr(string, substring)返回substring在string中出现的位置,没有返回0
ucase(string)转换成大写
lcase(string)转换成小写
left(string, length)从string中的左边开始取length个字符
length(string)string的长度
replace(str, search_str, replace_str)在str中用replace str替换search_str
strcmp(string, string)逐字符比较两字符串大小
substring(str, position[, length])从str的position开始,取length个字符
ltrim(string) rtrim(string) trim(string)取出前空格或后空格

使用示例

假设现在有一个学生表,其中包含学号、姓名、语文、数学、英语字段:

创建学生表:

create table stu(sn int primary key,
	name varchar(32) not null,
	chinese int not null,
	math int not null,
	english int not null
);

插入数据:

insert into stu values(22011, '张三', 70, 98, 50);
insert into stu values(22012, '李四', 95, 97, 70);
insert into stu values(22013, '王五', 80, 90, 60);

2.1 charset函数

获取stu表的name列的字符集

select charset(name) from stu;

结果:

mysql> select charset(name) from stu;
+---------------+
| charset(name) |
+---------------+
| utf8mb4       |
| utf8mb4       |
| utf8mb4       |
+---------------+
3 rows in set (0.00 sec)

2.2 concat函数

要求显示stu表中的信息,显示格式:“XXX的语文是XXX分,数学XXX分,英语XXX分”

select concat(name, '的语文是',chinese,'分,数学是',math,'分') as '分数' from
stu;

结果:

mysql> select concat(name, '的语文是',chinese,'分,数学是',math,'分') as '分数' from
    -> stu;
+------------------------------------------+
| 分数                                     |
+------------------------------------------+
| 张三的语文是70分,数学是98|
| 李四的语文是95分,数学是97|
| 王五的语文是80分,数学是90|
+------------------------------------------+
3 rows in set (0.00 sec)

2.3 length函数

求学生表中学生姓名占用的字节数

select length(name), name from stu;

结果:

mysql> select length(name), name from stu;
+--------------+--------+
| length(name) | name   |
+--------------+--------+
|            6 | 张三   |
|            6 | 李四   |
|            6 | 王五   |
+--------------+--------+
3 rows in set (0.00 sec)

2.4 replace函数

将stu表中所有名字中有 三 的替换成’三三’

select replace(name, '三', '三三') ,name from stu;

结果:

mysql> select replace(name, '三', '三三') ,name from stu;
+--------------------------------+--------+
| replace(name, '三', '三三')    | name   |
+--------------------------------+--------+
| 张三三                         | 张三   |
| 李四                           | 李四   |
| 王五                           | 王五   |
+--------------------------------+--------+
3 rows in set (0.00 sec)

2.5 substring函数

截取stu表中name字段的第二个到第三个字符

select substring(name, 2, 2), name from stu;

结果:

mysql> select substring(name, 2, 2), name from stu;
+-----------------------+--------+
| substring(name, 2, 2) | name   |
+-----------------------+--------+
|| 张三   |
|| 李四   |
|| 王五   |
+-----------------------+--------+
3 rows in set (0.00 sec)

2.6 lcase函数

以首字母小写的方式显示所有学生的姓名

select concat(lcase(substring(name, 1, 1)),substring(name,2)) from stu;

结果:

mysql> select concat(lcase(substring(name, 1, 1)),substring(name,2)) from stu;
+--------------------------------------------------------+
| concat(lcase(substring(name, 1, 1)),substring(name,2)) |
+--------------------------------------------------------+
| 张三                                                   |
| 李四                                                   |
| 王五                                                   |
+--------------------------------------------------------+
3 rows in set (0.00 sec)

三、数学函数

函数名称函数说明
abs(number)绝对值函数
bin(decimal_number)十进制转换二进制
hex(decimalNumber)转换成十六进制
conv(number, from_base, to_base)进制转换
ceiling(number)向上取整
floor(number)向下取整
format(number, decimal_place)格式化,保留小数位数
hex(decimalNumber)转换成十六进制
rand()返回随机浮点数,范围[0.0, 1.0)
mod(number, denominator)取模,求余

3.1 abs函数

绝对值

select abs(-100.2);

3.2 ceiling和floor函数

select ceiling(23.05), floor(24.7);

3.3 format函数

保留2位小数位数(小数四舍五入)

select format(12.3456, 2);

3.4 rand函数

产生随机数

select rand();

四、其他函数

user() 查询当前用户

select user();

结果:

+----------------+
| user()         |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)

md5(str)对一个字符串进行md5摘要,摘要后得到一个32位字符串

select md5('admin')

结果:

+----------------------------------+
| md5('admin')                     |
+----------------------------------+
| 21232f297a57a5a743894a0e4a801fc3 |
+----------------------------------+
1 row in set (0.00 sec)

database()显示当前正在使用的数据库

select database();

结果:

+------------+
| database() |
+------------+
| mydbs      |
+------------+
1 row in set (0.00 sec)

ifnull(val1, val2) 如果val1为null,返回val2,否则返回val1的值

select ifnull('abc', '123'), ifnull(null, '123');

结果:

mysql> select ifnull('abc', '123'), ifnull(null, '123');
+----------------------+---------------------+
| ifnull('abc', '123') | ifnull(null, '123') |
+----------------------+---------------------+
| abc                  | 123                 |
+----------------------+---------------------+
1 row in set (0.00 sec)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

冧轩在努力

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值