字符串函数:
#concat(字符串拼接)
select concat ('hello',' mysql');

lower(字符串转化为小写)upper(字符串转化为大写
select lower('Hello');
select upper('hello');

# lpad(左填充)rpad(右填充)注意格式
select lpad('01',5,'-');
select rpad('01',5,'-');
#trim 去除收尾空格(中间的还在)(去除的是第一个和最后一个空格)
#几遍前面有无数个空格也只去掉第一个
select trim(' hello mysql ');

# substring(str,start,len)
#注意这里字符串是从1开始的不是丛0开始的
select substring('hello mysql', 1, 5);

#将教师号更改形式计算机系为001,自动化002,信息003(如S1->S1001)(S2->S2)
update t set TNo =rpad(TNo,5,'001')where dept='计算机';
update t set TNo =rpad(TNo,5,'002')where dept='自动化';
update t set TNo =rpad(TNo,5,'003')where dept='信息';
select *from t;

数值函数
#ceil(向上取整)floor(向下取整)mod(余数)rand(0-1随机数)round(四舍五入)
select ceil(1.2);#只要小数位不是0就+1
select floor(1.9);
select mod(4,3);-- 注意是4,3不是4/3
select rand();
select round(2.5687,2);
select round(2.5687,3);
利用数据库生成6位随机数:
select lpad(round(rand()*1000000,0),6,'0');

日期函数
#curdate(返回当前日期),cubtime(返回当前时间),now(返回当前日期和时间
select curdate()as '当前日期';
select curtime()as '当前时间';
select now() as '当前日期和时间';



600秒以前的时间:
select month(now());
select date_add(now(),interval -600 second);//ed是-,ing是+或者省略

– #datediff(date1,date2)date1-date2
select datediff('2019.9.1','2022.04.03');

查询学生入校天数并按照天数倒序排序
select SN,datediff(curdate(),intime) as 'sum_day' from s order by sum_day desc;

流程函数
#流程函数【常用】
#if(value,t,f);三目运算
#ifnull(value1,value2);
#case when val1 then ... else ... end;
#case biaodashi when val1 then ... else ... end;
select if(5>20,'true','false');

select ifnull('','第一个值为空');#''不为空返回的是空白
select ifnull(null,'第一个值为null') as '判断框';


查询s表的姓名和专业,如果专业是计算机返回热门专业,其他则返回其他专业
【不涉及表达式的用以下的方法】
select
sn,
case dept when '计算机' then '热门专业'
#可以有多个when和then
else '其他专业' end as '专业类别'
from s;

#对成绩表sc进行操作,>=95为优秀,>=85为良好,>=75为中等,>=60为及格其他不及格,并显示学号
【表达式判断的用以下的方法】
select#
SNo,
case when Score>=95 then '优秀'
when Score>=85 then '良好'
when Score>=75 then '中等'
when Score>=60 then '及格'
else '不及格' end as '等级'#在尾部添加as
from SC;
select *from sc;

本节代码:
select database();
use students;
show tables;
select *from sc;
#字符串函数
#concat(字符串拼接)lower(字符串转化为小写)upper(字符串转化为大写
# lpad(左填充)rpad(右填充)trim(去除字符串头尾部空格)
# substring(str,start,len)
select concat ('hello',' mysql');
select lower('Hello');
select upper('hello');
# lpad(左填充)rpad(右填充)注意格式
select lpad('01',5,'-');
select rpad('01',5,'-');
#trim 去除收尾空格(中间的还在)(去除的是第一个和最后一个空格)
#几遍前面有无数个空格也只去掉第一个
select trim(' hello mysql ');
#注意这里字符串是从1开始的不是丛0开始的
select substring('hello mysql', 1, 5);
#将教师号更改形式计算机系为001,自动化002,信息003(如S1->S1001)(S2->S2)
update t set TNo =rpad(TNo,5,'001')where dept='计算机';
update t set TNo =rpad(TNo,5,'002')where dept='自动化';
update t set TNo =rpad(TNo,5,'003')where dept='信息';
select *from t;
#数值函数
#ceil(向上取整)floor(向下取整)mod(余数)rand(0-1随机数)round(四舍五入)
select ceil(1.2);#只要小数位不是0就+1
select floor(1.9);
select mod(4,3);-- 注意是4,3不是4/3
select rand();
select round(2.5687,2);
select round(2.5687,3);
-- 利用数据库生成6位随机数:
select lpad(round(rand()*1000000,0),6,'0');
#日期函数
#curdate(返回当前日期),cubtime(返回当前时间),now(返回当前日期和时间
# year/month/dat(date)(获取指定的年月日
# dete_add(date,interval,expr 时间单位)返回一个日期/时间加上一个时间间隔expr后的时间值
#datediff(date1,date2)返回两个时间之间的天数
alter table s add intime varchar(10);
select *from s;
select curdate()as '当前日期';
select curtime()as '当前时间';
select now() as '当前日期和时间';
select month(now());
select date_add(now(),interval -600 second);//ed是-,ing是+或者省略
-- #datediff(date1,date2)date1-date2
select datediff('2019.9.1','2022.04.03');
查询学生入校天数并按照天数倒序排序
select SN,datediff(curdate(),intime) as 'sum_day' from s order by sum_day desc;
#流程函数【常用】
#if(value,t,f);三目运算
#ifnull(value1,value2);
#case when val1 then ... else ... end;
#case biaodashi when val1 then ... else ... end;
select if(5>20,'true','false');
select ifnull('','第一个值为空');#''不为空返回的是空白
select ifnull(null,'第一个值为null') as '判断框';
查询s表的姓名和专业,如果专业是计算机返回热门专业,其他则返回其他专业
select
sn,
case dept when '计算机' then '热门专业'
#可以有多个when和then
else '其他专业' end as '专业类别'
from s;
#对成绩表sc进行操作,>=95为优秀,>=85为良好,>=75为中等,>=60为及格
其他不及格,并显示学号
select#
SNo,
case when Score>=95 then '优秀'
when Score>=85 then '良好'
when Score>=75 then '中等'
when Score>=60 then '及格'
else '不及格' end as '等级'#在尾部添加as
from SC;
select *from sc;










