红茶的个人站点

  • 首页
  • 专栏
  • 开发工具
  • 其它
  • 隐私政策
Awalon
Talk is cheap,show me the code.
  1. 首页
  2. 专栏
  3. MySQL学习笔记
  4. 正文

MySQL 从入门到精通 2:函数

2025年7月31日 14点热度 0人点赞 0条评论

字符串函数

函数 功能
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;

参考资料

  • 黑马程序员 MySQL数据库入门到精通

本作品采用 知识共享署名 4.0 国际许可协议 进行许可
标签: MySQL 函数
最后更新:2025年7月31日

魔芋红茶

加一点PHP,加一点Go,加一点Python......

点赞
< 上一篇
下一篇 >

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复

COPYRIGHT © 2021 icexmoon.cn. ALL RIGHTS RESERVED.
本网站由提供CDN加速/云存储服务

Theme Kratos Made By Seaton Jiang

宁ICP备2021001508号

宁公网安备64040202000141号