MySQL 从入门到精通 2:函数

MySQL 从入门到精通 2:函数

字符串函数

函数功能
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位置起的len个长度的字符串

连接字符串

select concat('Hello',' ','World');

转为小写

select lower('Hello World');

转为大写

select upper('Hello World');

左填充

select lpad('1',5,'0');

右填充

select rpad('1',5,'0');

去除空白符

select trim(' Hello World   ');

截取子字符串

select substring('Hello World', 1, 5);

需要注意的是,SQL 函数中字符串偏移量是从1开始的,而非编程语言中常见的从0开始

数值函数

函数功能
CEIL(x)向上取整
FLOOR(x)向下取整
MOD(x)返回 x/y 的余数
RAND()返回 0~1 之间的随机数
ROUND(x,y)对x保留y位小数(四舍五入)

向上取整

select ceil(1.1);
-- 结果为 2

向下取整

select floor(1.1);
-- 结果为 1

取余

select mod(9,4);
-- 结果为 1

随机数

select rand();
-- 结果为 0.6149889273708583

保留小数位

select round(1.234345, 2);
-- 结果为 1.23

案例,利用 MySQL 函数获取一个六位的随机数:

select lpad(round(rand()*1000000,0),6,'0');

日期函数

函数功能
CURDATE()当前日期
CURTIME()当前时间
NOW()当前日期和时间
YEAR(date)获取指定日期的年份
MONTH(date)获取指定日期的月份
DAY(date)获取指定日期的日期
DATE_ADD(date, INTERVAL expr type)返回一个日期/时间值加上一个时间间隔 expr 后的时间值
DATEDIFF(date1, date2)返回起始时间 date1 和结束时间 date2 之间的天数

当前日期

select curdate();
-- 返回 2025-07-31

当前时间

select curtime();
-- 返回 12:23:19

当前日期和时间

select now();
-- 返回 2025-07-31 12:24:03

指定日期的年份

select year(now());
-- 返回 2025

指定日期的月份

select month(now());
-- 返回 7

指定日期的日期

select day(now());
-- 返回 31

日期累加

select date_add('2025-01-01', interval 5 day );
-- 结果 2025-01-06
select date_add('2025-01-01', interval 5 month );
-- 结果 2025-06-01
select date_add('2025-01-01', interval 5 year );
-- 结果 2030-01-01

计算日期差值

select datediff('2025-05-01', '2025-03-07');
-- 结果 55

需要注意的是,这个函数计算时候是用前者减去后者,所以是可能出现负值的:

select datediff('2025-03-07', '2025-05-01');
-- 结果 -55

流程函数

函数功能
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] WHERN [val1] THEN [res1] … ELSE [default] END如果 expr 的值等于 val1,返回 res1, … 否则返回 default

IF:

select if(true, 'true', 'false');
-- 返回 true
select if(false, 'true', 'false');
-- 返回 false

IFNULL:

select ifnull(null, 'is null');
-- 返回 is null
select ifnull('ok', 'is null');
-- 返回 ok

CASE:

示例表数据:

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);

根据成绩显示优秀、及格或不及格:

select
    id,id, name, math,
    (case when math>=85 then '优秀' when math>=60 then '及格' else '不及格' end) as math_level,
    english,
    (case when english>=85 then '优秀' when english>=60 then '及格' else '不及格' end) as englisth_level,
    chinese,
    (case when chinese>=85 then '优秀' when chinese>=60 then '及格' else '不及格' end) as chinese_level
from score;

参考资料

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值