MySQL :7_函数

函数

一.日期函数

image-20250326131448917

  • 获得年月日
select current_date();
+----------------+
| current_date() |
+----------------+
| 2017-11-19	 |
+----------------+
  • 获得时分秒:
select current_time();
+----------------+
| current_time() |
+----------------+
| 13:51:21 		 |
+----------------+
  • 获得时间戳:
select current_timestamp();
+---------------------+
| current_timestamp() |
+---------------------+
| 2017-11-19 13:51:48 |
+---------------------+

等价于

mysql> select now();
+---------------------+
| now()               |
+---------------------+
| 2025-03-30 12:05:46 |
+---------------------+
  • 在日期的基础上加日期:
select date_add('2017-10-28', interval 10 day);
+-----------------------------------------+
| date_add('2017-10-28', interval 10 day) |
+-----------------------------------------+
|			 2017-11-07        			  |
+-----------------------------------------+
select date_add(now(),interval 10 day );
+----------------------------------+
| date_add(now(),interval 10 day ) |
+----------------------------------+
| 2025-04-09 12:08:47              |
+----------------------------------+
  • 在日期的基础上减去时间:
mysql> select date_sub('2017-10-1', interval 2 day);
+---------------------------------------+
| date_sub('2017-10-1', interval 2 day) |
+---------------------------------------+
| 2017-09-29                            |
+---------------------------------------+
  • 计算两个日期之间相差多少天:
mysql> select datediff('2017-10-10', '2016-9-1');
+------------------------------------+
| datediff('2017-10-10', '2016-9-1') |
+------------------------------------+
|                                404 |
+------------------------------------+

案例-1:

  • 创建一张表,记录生日
create table tmp(
    id int primary key auto_increment,
    birthday date
);
  • 添加当前日期:
insert into tmp(birthday) values(current_date());

插入current_date(),current_time(),current_timestamp()都可以,有点像隐式类型转换;但不要这么做插入非current_time()的时候前面加date()自己转换一下

mysql> select * from tmp;
+----+------------+
| id | birthday   |
+----+------------+
|  1 | 2025-03-30 |
+----+------------+

案例-2:

  • 创建一个留言表
mysql> create table msg (
    id int primary key auto_increment,
    content varchar(30) not null,
    sendtime datetime
);
  • 插入数据
mysql> insert into msg(content,sendtime) values('hello1', now());
mysql> insert into msg(content,sendtime) values('hello2', now());
mysql> select * from msg;
+----+---------+---------------------+
| id | content | 	sendtime		 |
+----+---------+---------------------+
| 1  |  hello1 | 2017-11-19 14:12:20 |
| 2  |  hello2 | 2017-11-19 14:13:21 |
+----+---------+---------------------+
  • 显示所有留言信息,发布日期只显示日期,不用显示时间
select content,date(sendtime) from msg;
  • 请查询在2分钟内发布的帖子
select * from msg where date_add(sendtime, interval 2 minute) > now();
理解:
------------------------------|-----------|-------------|------------------
						    初始时间 	  now() 	   初始时间+2min

二.字符串函数

image-20250330184012260

案例:

  • 获取emp表的ename列的字符集
select charset(ename) from EMP;
  • 要求显示exam_result表中的信息,显示格式:“XXX的语文是XXX分,数学XXX分,英语XXX分”
select concat(name, '的语文是',chinese,'分,数学是',math,'分') as '分数' from student;
  • 求学生表中学生姓名占用的字节数
select length(name), name from student;

注意:length函数返回字符串长度,以字节为单位。如果是多字节字符则计算多个字节数;如果是单字节字符则算作一个字节。比如:字母,数字算作一个字节,中文表示多个字节数(与字符集编码有关)

  • 将EMP表中所有名字中有S的替换成’上海’
select replace(ename, 'S', '上海') ,ename from EMP;
  • 截取EMP表中ename字段的第二个到第三个字符
select substring(ename, 2, 2), ename from EMP;
  • 以首字母小写的方式显示所有员工的姓名
select concat(lcase(substring(ename, 1, 1)),substring(ename,2)) from EMP;

三.数学函数

image-20250330184027372

image-20250330184048677

  • 绝对值
select abs(-100.2);
  • 向上取整
select ceiling(23.04);
  • 向下取整
select floor(23.7);
  • 保留2位小数位数(小数四舍五入)
select format(12.3456, 2);
  • 产生随机数
select rand();

image-20250330184337576

  • 十进制转二进制
select conv (9,10,8);
+---------------+
| conv (9,10,8) |
+---------------+
| 11            |
+---------------+
  • 十模三
select mod (10,3);
+------------+
| mod (10,3) |
+------------+
|          1 |
+------------+

四.其它函数

  • user() 查询当前用户
select user();
  • md5(str)对一个字符串进行md5摘要,摘要后得到一个32位字符串(相对于一种加密)
select md5('admin')
+----------------------------------+
| md5('admin') 					   |
+----------------------------------+
| 21232f297a57a5a743894a0e4a801fc3 |
+----------------------------------+
  • database()显示当前正在使用的数据库
select database();
  • password()函数,MySQL数据库使用该函数对用户加密
select password('root');
+-------------------------------------------+
| password('root') 							|
+-------------------------------------------+
| *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
+-------------------------------------------+
  • ifnull(val1, val2) 如果val1为null,返回val2,否则返回val1的值
select ifnull('abc', '123');
+----------------------+
| ifnull('abc', '123') |
+----------------------+
| abc 				   |
+----------------------+
1 row in set (0.01 sec)

select ifnull(null, '123');
+---------------------+
| ifnull(null, '123') |
+---------------------+
| 123 				  |
+---------------------+
1 row in set (0.00 sec)

五.实战OJ

牛客:查找字符串’10,A,B’ 中逗号’,'出现的次数cnt

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值