MySQL ---- 函数

3 篇文章 0 订阅
3 篇文章 0 订阅

函数

  函数是指一段可以直接被另一段程序调用的程序或代码。MySQL 内置了很多函数,开发人员只需要调用使用即可。查询语句中已经使用过了一些聚合函数。下面还有些常用的函数需要掌握。

  • 函数应用场景举例
函数应用场景
  • 函数的分类

  ① 字符串函数 ② 数值函数 ③ 日期函数 ④ 流程函数

1、字符串函数

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

函数功能
concat(s1,s2,…,sn)字符串拼接,将s1,s2,…,sn拼接成一个字符串
lower(str)将字符串 str 全部转为小写
upper(str)将字符串 str 全部转为大写
lpad(str,n,pad)左填充,用字符串 pad 对 str 的左边进行填充,达到 n 个字符串长度
rpad(str,n,pad)右填充,用字符串 pad 对 str 的右边进行填充,达到 n 个字符串长度
trim(str)去掉字符串头部和尾部的空格
substring(str,start,len)返回字符串 str 从 start 位置(从1开始)起的 len 个长度的字符串
  • 字符串函数示例
-- concat(s1,s2,...,sn)
select concat('hello','mysql');     -- hellomysql
-- lower(str)
select lower('Hello');              -- hello
-- upper(str)
select upper('Hello');              -- HELLO
-- lpad(str,n,pad)
select lpad('01',5,'0');            -- 00001
-- rpad(str,n,pad)
select rpad('01',5,'0');            -- 01000
-- trim(str)
select trim('  001  100  ');        -- 001  100
-- substring(str,start,len)
select substring('hellomysql',1,2); -- he
-- 案例:由于业务需求变更,企业员工的工号workid,统一为5位数,目前不足5位数的全部在前面补0。
-- 比如: 1号员工的工号应该为00001。
update emp set workid = lpad(workid,5,'0');

2、数值函数

  常见的数值函数如下:

函数功能
ceil(x)向上取整
floor(x)向下取整
mod(x,y)返回 x/y 的模(取余数)
rand()返回 0-1 内的随机数
round(x,y)求参数 x 的四舍五入的值,保留 y 位小数
  • 数值函数示例
-- ceil(x)
select ceil(1.0);  -- 1
select ceil(1.2);  -- 2
-- floor(x)
select floor(1.0);  -- 1
select floor(1.9);  -- 1
-- mod(x,y)
select mod(3,5);    -- 3(3÷5=0余3)
select mod(5,3);    -- 2(5÷3=1余2)
-- rand()
select rand();      -- 0.9802188463324857(随机生成)
select rand();      -- 0.2877062636313087(随机生成)
-- round(x,y)
select round(0.98021,1);    -- 1.0
select round(0.98021,2);    -- 0.98
-- 案例 生成6位随机验证码
select substring(rand(),3,6);   -- 626255(随机 6 位数)

3、日期函数

  常见的日期函数如下:

函数功能
curdate()返回当前日期
curtime()返回当前时间
now()放回当前日期和时间
year(date)获取指定 date 的年份
month(date)获取指定 date 的月份
day(date)获取指定 date 的日期
date_add(date,interval expr type)返回一个日期/时间值加上一个时间间隔 expr 后的时间值
datediff(date1,date2)返回起始时间 date1 和结束时间 date2 之间的天数
  • 日期函数示例
-- curdate()
select curdate();               -- 2023-05-09
-- curtime()
select curtime();               -- 12:23:52
-- now()
select now();                   -- 2023-05-09 12:24:17
-- year(date)
select year(now());             -- 2023
select year('2001-01-04');      -- 2001
-- month(date)
select month(now());             -- 5
select month('2001-01-04');      -- 1
-- day(date)
select day(now());             -- 9
select day('2001-01-04');      -- 4
-- date_add(date,interval expr type)
select date_add(now(),interval 4 day);      -- 2023-05-13 12:29:32 (推迟4天)
select date_add(now(),interval 4 month);    -- 2023-09-09 12:30:07 (推迟4个月)
select date_add(now(),interval 4 year);     -- 2027-05-09 12:30:43 (推迟4年)
-- datediff(date1,date2): date1 - date2
select datediff(date_add(now(),interval 59 day),now()); -- 59
-- 案例:计算自己出生了多少天
select datediff(now(),'1995-12-05');        -- 10017

4、流程函数

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

函数功能
if(value,t,f)如果 value 为 true ,则返回 t,否则返回 f
ifnull(value1,value2)如果 value1 不为空,则返回 value1,否则返回 value2
case when [val1] then [res1] … else [default] end如果 val1 为 true,返回 res1,… 否则返回 default 默认值
case [expr] when [val1] then [res1] … else [default] end如果 expr 的值等于 val1,返回 res1,… 否则返回 default 默认值
-- 流程控制函数
-- if
select if(true,'Ok','Error');        -- Ok
select if(false,'Ok','Error');       -- Error
select if(1,'Ok','Error');           -- Ok
select if(0,'Ok','Error');           -- Error
select if('','Ok','Error');          -- Error
select if('1','Ok','Error');         -- Ok
-- ifnull
select ifnull('','Default');         -- (空字符串)
select ifnull(1,'Default');          -- 1
select ifnull(true,'Default');       -- 1
select ifnull(null,'Default');       -- Default
-- case when then else end
-- 根据城市展示一线城市和非一线城市
select name,
       (case workaddress when '北京' then '一线城市' when '上海' then '一线城市'else '非一线城市' end) 工作地址
from emp;
-- 根据学生不同分数段展示成绩等级
select id,name,
       (case when math >= 85 then '优秀' when math >= 60 then '及格' else '不及格' end) 数学,
       (case when chinese >= 85 then '优秀' when chinese >= 60 then '及格' else '不及格' end) 语文,
       (case when english >= 85 then '优秀' when english >= 60 then '及格' else '不及格' end) 英语
from score;
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

ItDaChuang

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

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

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

打赏作者

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

抵扣说明:

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

余额充值