欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 财经 > 产业 > 【JavaWeb后端学习笔记】MySQL的常用函数(字符串函数,数值函数,日期函数,流程函数)

【JavaWeb后端学习笔记】MySQL的常用函数(字符串函数,数值函数,日期函数,流程函数)

2025/2/5 20:24:37 来源:https://blog.csdn.net/wrjwww/article/details/144886931  浏览:    关键词:【JavaWeb后端学习笔记】MySQL的常用函数(字符串函数,数值函数,日期函数,流程函数)

MySQL函数

  • 1、字符串函数
  • 2、数值函数
  • 3、日期函数
  • 4、流程函数

1、字符串函数

函数说明
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 个长度的字符串,索引从 1 开始
-- concat
select concat('hello', 'MySQL');-- lower
select lower('Hello');-- upper
select upper('Hello');-- lpad
select lpad('hhh', 5, '-');-- rpad
select rpad('hhh', 5, '-');-- trim
select trim('  Hello MySQL!  ');-- substring
select substring('Hello MySQL', 1, 5);

2、数值函数

函数说明
ceil(x)向上取整
floor(x)向下取整
mod(x, y)返回 x/y 的模
rand()返回 0~1 内的随机数
round(x, y)求参数 x 的四舍五入的值,保留 y 为小数
-- ceil
select ceil(1.1);-- floor
select floor(1.1);-- mod
select mod(7, 4);-- rand
select rand();-- round
select round(1.254, 1);

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();-- curtime
select curtime();-- now
select now();-- YEAR
select YEAR(now());-- MONTH
select MONTH(now());-- DAY
select DAY(now());-- DATE_ADD
select DATE_ADD(now(), INTERVAL 10 DAY);-- datediff
select datediff('2021-10-01', '2021-06-01');

4、流程函数

函数说明
if(value, t, f)如果 value 为 true, 则返回 t, 否则返回 f
ifnull(value1, value2)如果 value1 不为空, 返回 value1, 否则返回value2
case when [val1] then [res1] when [val2] then [res2] … else [default] end如果 val1 为 true, 返回 res1,…,否则返回 default 默认值
case [expr] when [val1] then [res1] when [val2] then [res2] … else [default] end如果 expr 的值等于 val1,返回 res1,…,否则返回 default 默认值
-- if(value, t, f)
select if(true, 'True', 'False');
select if(false, 'True', 'False');-- ifnull(value1, value2)
select ifnull('OK', 'Default');
select ifnull('', 'Default');
select ifnull(null, 'Default');-- case when [val1] then [res1] when [val2] then [res2] ... else [default] end
select id, name,(case when math >= 85 then '优秀' when math >= 60 then '及格' else '不及格' end) as math,(case when english >= 85 then '优秀' when english >= 60 then '及格' else '不及格' end) as english,(case when chinese >= 85 then '优秀' when chinese >= 60 then '及格' else '不及格' end) as chinese
from score;-- case [expr] when [val1] then [res1] when [val2] then [res2] ... else [default] end
selectid,name,(case address when '北京' then '一线城市' when '上海' then '一线城市' else '二线城市' end) as address
from tb_emp;

版权声明:

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

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