各位用户为了找寻关于MySQL实例精讲单行函数以及字符数学日期流程控制的资料费劲了很多周折。这里教程网为您整理了关于MySQL实例精讲单行函数以及字符数学日期流程控制的相关资料,仅供查阅,以下为您介绍关于MySQL实例精讲单行函数以及字符数学日期流程控制的详细内容
一、字符函数
1、大小写控制函数
①upper()
:转换成大写
select
upper
(
'hello'
);
②lower()
:转换成小写
select
lower
(
'hello'
);
2、字符控制函数
①length()
:获取参数值的字节个数
select
length(
'叶绿体不忘呼吸aaaa'
);
②concat()
:拼接字符串
select
concat(
'hello'
,
'世界'
)
as
result;
③substr()
:截取(mysql里索引是从1开始的,而不是0)
#从第4个开始截取,截取后面全部
select
substr(
'我相信光'
,4);
#从第1个开始截取,截取3个
select
substr(
'我相信光'
,1,3);
④instr()
:返回子串在主串中第一次出现的索引,如果不存在,则返回0
select
instr(
'国足10月13日客战沙特'
,
'沙特'
)
as
result;
⑤trim()
:去除字符串前后某字符
select
trim(
'a'
from
'aaaaa叶aa绿体aaaaa'
)
as
result;
#去空格
select
trim(
' 叶aa绿体a '
)
as
result;
⑥lpad()
:用指定的字符左填充指定长度,rpad()
则是右填充
select
lpad(
'叶绿体'
,9,
'a'
)
as
result;
⑦replace()
:替换
select
replace
(
'a叶aaa绿体aaaa'
,
'a'
,
'b'
)
as
result;
二、数学函数
①round()
:四舍五入
#默认保留一位小数
select
round(1.62)
as
result;
#保留两位小数
select
round(1.627,2)
as
result;
②ceil()
:向上取整,返回大于等于该数的最小整数
select
ceil(1.002)
as
result;
③floor()
:向下取整,返回小于等于该数的最大整数
select
floor(1.002)
as
result;
④truncate()
:截断
select
truncate
(1.699,1)
as
result;
⑤mod()
:取余,等价于%
select
mod(10,-3)
as
result;
三、日期函数
①now():返回当前系统的日期和时间
? 1select
now();
②curdate():返回当前系统日期,不包含时间
? 1select
curdate();
③curtime():返回当前系统时间,不包括日期
? 1select
curtime();
⑦yrear()
:获取指定日期字段的年
select
year
(now());
select
year
(
'2021-09-30'
)
as
result;
⑧month()
:获取指定日期字段的月,monthname()
则可以返回月份英文
select
month
(
'2021-09-30'
)
as
result;
select
monthname(
'2021-09-30'
)
as
result;
日,小时,分钟,秒钟都可以同上
⑨str_to_date()
:将字符按照指定的格式转为日期
#相当于是解析:两个参数格式要匹配
select
str_to_date(
'9-30 2021'
,
'%m-%d %y'
)
as
result;
⑩date_format()
:将日期转换成字符
#相当于是格式化
select
date_format(now(),
'%y年%m月%d日'
)
as
result;
四、其他函数
? 1 2 3 4 5 6#查看版本
select
version();
#查看数据库
select
database
();
#查看用户
select
user
();
五、流程控制函数
①if()
:判断,第一个参数是条件,第二个是true的返回,第三个是false的返回
select
if(10>5,
'大'
,
'小'
)
as
result;
②case()
使用一:类似于java中switch
case
要判断的
when
常量1
then
语句1;或者要显示的值1
...
else
语句;或者要显示的值
end
示例
? 1 2 3 4 5 6 7 8 9 10#示例为要显示的值,不加‘;'
select
`last_name`,`salary`,`department_id`,
case
`department_id`
when
100
then
`salary`*(1+0.8)
when
90
then
`salary`*(1+0.6)
when
80
then
`salary`*(1+0.4)
when
70
then
`salary`*(1+0.2)
else
`salary`
end
as
最终工资
from
employees;
③case()
使用一:类似于java中多重if
case
when
条件1
then
select
语句1;或者要显示的值1
...
else
语句;或者要显示的值
end
示例
? 1 2 3 4 5 6 7 8 9#示例为要显示的值,不加‘;
'
select `last_name`,`salary`,
case
when `salary`>20000 then '
a级别
'
when `salary`>15000 then '
b级别
'
when `salary`>10000 then '
c级别
'
else '
d级别'
end
as
等级
from
employees;
到此这篇关于mysql深度精讲单行函数以及字符数学日期流程控制的文章就介绍到这了,更多相关mysql 单行函数 内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
原文链接:https://blog.csdn.net/m0_46653805/article/details/120699370