欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 财经 > 产业 > MySQL的基础语法2(函数-字符串函数、数值函数、日期函数和流程函数 )

MySQL的基础语法2(函数-字符串函数、数值函数、日期函数和流程函数 )

2025/4/2 21:22:00 来源:https://blog.csdn.net/weixin_68453343/article/details/146640364  浏览:    关键词:MySQL的基础语法2(函数-字符串函数、数值函数、日期函数和流程函数 )

目录

一、字符串函数

1.常见字符串函数

​编辑 2.字符串函数的基本使用

3.字符串函数的数据库案例演示

二、数值函数

1.常见数值函数(如下):

 2.数值函数的基本使用

3.数值函数的数据库案例演示

三、日期函数

1.常见的日期函数

2.日期函数的基本使用

3.日期函数的数据库案例演示 

四、流程函数 

1.常见的流程函数

 2.流程函数的基本使用

3.流程函数的数据库案例演示


函数是指一段可以直接被另一段程序调用的程序或代码。 也就意味着,这一段程序或代码MySQL中已经给我们提供了,我们要做的就是在合适的业务场景调用对应的函数完成对应的业务需求即可。

一、字符串函数

1.常见字符串函数

MySQL中内置了很多字符串函数,常用的几个如下:

 2.字符串函数的基本使用

案例如下:

A. concat : 字符串拼接
select concat('Hello' , ' MySQL');B. lower : 全部转小写
select lower('Hello');C. upper : 全部转大写
select upper('Hello');D. lpad : 左填充
select lpad('01', 5, '-'); --  ---01E. rpad : 右填充
select rpad('01', 5, '-');  -- 01---F. trim : 去除空格
select trim(' Hello  MySQL ');  -- Hello MySQLG. substring : 截取子字符串
select substring('Hello MySQL',1,5); --Hello

3.字符串函数的数据库案例演示

 题目要求:由于业务需求变更,企业员工的工号,统一为5位数,目前不足5位数的全部在前面补0。比如: 1号员 工的工号应该为00001。

首先创建员工数据库(代码如下):

create table emp(id int comment '编号',workno varchar(10) comment '工号',name varchar(10) comment '姓名',gender char(1) comment '性别',age tinyint unsigned comment '年龄',idcard char(18) comment '身份证号',workaddress varchar(50) comment '工作地址',entrydate date comment '入职时间')comment '员工表';INSERT INTO emp (id, workno, name, gender, age, idcard, workaddress, entrydate)
VALUES (1, '00001', '柳岩', '女', 20, '123456789012345678', '北京', '2000-01-01'),(2, '00002', '张无忌', '男', 18, '123456789012345670', '北京', '2005-09-01'),(3, '00003', '韦一笑', '男', 38, '123456789712345670', '上海', '2005-08-01'),(4, '00004', '赵敏', '女', 18, '123456757123845670', '北京', '2009-12-01'),(5, '00005', '小昭', '女', 16, '123456769012345678', '上海', '2007-07-01'),(6, '00006', '杨逍', '男', 28, '12345678931234567X', '北京', '2006-01-01'),(7, '00007', '范瑶', '男', 40, '123456789212345670', '北京', '2005-05-01'),(8, '00008', '黛绮丝', '女', 38, '123456157123645670', '天津', '2015-05-01'),(9, '00009', '范凉凉', '女', 45, '123156789012345678', '北京', '2010-04-01'),(10, '00010', '陈友谅', '男', 53, '123456789012345670', '上海', '2011-01-01'),(11, '00011', '张士诚', '男', 55, '123567897123465670', '江苏', '2015-05-01'),(12, '00012', '常遇春', '男', 32, '123446757152345670', '北京', '2004-02-01'),(13, '00013', '张三丰', '男', 88, '123656789012345678', '江苏', '2020-11-01'),(14, '00014', '灭绝', '女', 65, '123456719012345670', '西安', '2019-05-01'),(15, '00015', '胡青牛', '男', 70, '12345674971234567X', '西安', '2018-04-01'),(16, '00016', '周芷若', '女', 18, null, '北京', '2012-06-01');

实现代码如下:

 update emp set workno = lpad(workno, 5, '0');

二、数值函数

1.常见数值函数(如下):

 2.数值函数的基本使用

A. ceil:向上取整
select ceil(1.1);  -- 2B. floor:向下取整
select floor(1.9);  -- 1C. mod:取模
select mod(7,4);   --  3 求余D. rand:获取随机数
select rand();  --  0-1之间  [0, 1)E. round:四舍五入
select round(2.344,2);  -- 对前面的数进行四舍五入,保留两位小数

3.数值函数的数据库案例演示

案例: (演示的表与一.3的案例同样)通过数据库的函数,生成一个六位数的随机验证码。并生成新的字段,然后加入数据库

ALTER TABLE emp ADD  rand_data int  COMMENT "随机数";
update emp set rand_data = rpad(round(rand()*1000000 , 0), 6, '0');

三、日期函数

1.常见的日期函数

2.日期函数的基本使用

A. curdate:当前日期
select curdate();  -- 2025-03-29B. curtime:当前时间  
select curtime();  -- 11:34:59C. now:当前日期和时间
select now();   -- 2025-03-29 11:35:16  D. YEAR , MONTH , DAY:当前年、月、日select YEAR(now());  -- 2025select MONTH(now());  -- 3select DAY(now());   -- 29E. date_add:增加指定的时间间隔
select date_add(now(), INTERVAL 70 YEAR );   -- 当前时间往后推70年  -- 2095-03-29 11:38:36F. datediff:获取两个日期相差的天数  -- 第一个时间减去第二个时间
select datediff('2021-10-01', '2021-12-01');  --  -61

3.日期函数的数据库案例演示 

案例: 查询所有员工的入职天数,并根据入职天数倒序排序。(演示的表与一.3的案例同样)

思路: 入职天数,就是通过当前日期 - 入职日期,所以需要使用datediff函数来完成。

select name, datediff(curdate(), entrydate) as 'entrydays' from emp order by entrydays desc;

四、流程函数 

流程函数也是很常用的一类函数,可以在SQL语句中实现条件筛选,从而提高语句的效率。

1.常见的流程函数

 2.流程函数的基本使用

A. if
-- IF(value, t, f)	如果 value 为 true,则返回 t,否则返回 f
select if(false, 'Ok', 'Error');  -- ErrorB. ifnull
-- IFNULL(value1, value2)	如果 value1 不为空,返回 value1,否则返回 value2
select ifnull('Ok','Default');  -- Ok
select ifnull('','Default');  --  返回前面的空字符串
select ifnull(null,'Default');  --  DefaultC. case when then else end
-- CASE WHEN [ val1 ] THEN [res1] ... ELSE [ default ] END	如果 val1 为 true,返回 res1,... 否则返回 default 默认值-- 需求: 查询emp表的员工姓名和工作地址 (北京/上海 ----> 一线城市 , 其他 ----> 二线城市)selectname,( case workaddress when '北京' then '一线城市' when '上海' then '一线城市' else 
'二线城市' end ) as '工作地址'from emp;-- 张无忌,一线城市
-- 韦一笑,一线城市

3.流程函数的数据库案例演示

create table score(id int comment 'ID',name varchar(20) comment '姓名',math int comment '数学',english int comment '英语',chinese int comment '语文') comment '学员成绩表';insert into score(id, name, math, english, chinese) VALUES (1, 'Tom', 67, 88, 95 
), (2, 'Rose' , 23, 66, 90),(3, 'Jack', 56, 98, 76);

 按分数段进行优秀和及格展示

selectid,name,(case when math >= 85 then '优秀' when math >=60 then '及格' else '不及格' end ) '数学',(case when english >= 85 then '优秀' when english >=60 then '及格' else '不及格'end ) '英语',(case when chinese >= 85 then '优秀' when chinese >=60 then '及格' else '不及格' end ) '语文'from score;

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词