#数学函数、日期函数、其它函数【补充】、流程控制函数【补充】(可以实现一些分支函数)
#数学函数
##一、round 四舍五入
select round(1.65);(输出:2)
select round(1.45);(输出:1)
select round(-1.45);(输出:-1)
select round(-1.65);(输出:-2)
###特别地,
select round(-1.567,2);(输出:-1.57)
##二、ceil 向上取整,返回>=该参数的最小整数
select ceil(1.002);(输出:2)
select ceil(1.00);(输出:1)
select ceil(-1.02);(输出:-1)
##三、floor 向下取整,返回<=该参数的最大整数
select floor(9.99);(输出:9)
select floor(-9.99);(输出:-10)
##四、truncate截断
select truncate(1.65,1);(输出:1.6)
select truncate(1.69999,1);(输出:1.6)
##五、mod取余
select mod(10,3);
跟select 10%3;一样
假如是负数,select mod(-10,-3);(输出:-1)
select mod(-10,3);(输出:-1)
select mod(10,-3);(输出:1)
和Java一样,如果被除数(10)为正,结果为正;被除数如果为负,结果为负;
###原理:它的算法
mod(a,b):a-a/bb一样;此处a/b如果都是整数那么算出来的结果是取整的,如果a为10,b为3
即是10-33=1;如果a为-10,b为3,即是-10+3*3=-1;
#日期函数
##一、now 返回当前系统日期+时间
select now ();
##二、curdate 返回当前系统日期,不包含时间
select curdate();
##三、curtime 返回当前时间,不包含日期
select curtime();
##可以获取指定的部分,年(year)、月(month)、日(day)、小时(hour)、分钟(minute)、秒(second)
select year(now()) 年;
select year('1998-1-1') 年;(输出:1998)
select month(now()) 月;
如果想要月份是英文,那么
select monthname(now()) 月;
##四、str_to_date:将日期格式的字符转换成指定格式的日期
str_to_date('9-13-1999','%m-%d-%Y'); (输出:1999-09-13)
str_to_date('1998-3-2','%Y-%c-%d') as out_put;(输出:1998-03-02)
##查询入职日期为1992-4-3的员工信息
select * from employees where hiredate = '1992-4-3';
select * from employees where hiredate =str_to_date('4-3 1992','%c-%d %Y');
###代表的符号
- %Y(四位的年份)%y(2位的年份)%m(月份(01,02,03,…11,12))%c(月份(1,2,…,11,12))%d(日(01,02,…))%H(小时(24小时制))%h(小时(12小时制))%i(分钟(00,01,…59))%s(秒(00,01,…59))
##五、date_format:将日期转换成字符
date_format('2018/6/6','%Y年%m月%d日')(输出:2018年06月06日)
select date_format(now(),'%y年%m月%d日') as out_put;(输出:17年09月29日)
##查询有奖金的员工名和入职日期(xx月/xx日 xx年)
select last_name, date_format(hiredate, '%m月/%d日 %y年')from employees where commission_pct is not null;
#其他函数
select version();
select database();
select user();
#流程控制函数
##一、if函数:if else 的效果
select if(10>5,'大','小');(输出:大)
(10>5,true返回大,FALSE返回小)
select last_name,commission_pct,if(commission_pct is null,'没奖金,呵呵','有奖金,嘻嘻') 备注 from employees;
##二、case函数(可以说是函数,也可以不称为函数)(功能结构)(可以作为单战的一条语句)(也可搭配select充当表达式)
(没学过Java,实在不懂,记下)
##1. 效果一:switch case 类似 (比较适合等值判)
mysql中
case 要判断的字段或表达式
when 常量1 then 要显示的值1或语句1;(如果是语句加分号,不是语句只是一个值,不用加分号)(这里可以有多个)
when 常量2 then 要显示的值2或语句2;
…
else 要显示的值n或语句n;
end
##案例:查询员工的工资,要求
部门号=30,显示的工资为1.1倍
部门号=40,显示的工资为1.2倍
部门号=50,显示的工资为1.3倍
其他部门,显示的工资为原工资
(case相当于表达式)
select salary 原始工资,department_id, case department_id when 30 then salary*1.1 when 40 then salary*1.2 when 50 then salary*1.3 else salary end as 新工资 from employees;
##2. case函数的使用:类似于多重if(判断的是区间,即大于小于的判断)(所以case后面不可能就把条件写好了,只能每个条件单独的写)
mysql中
case
when 条件1 then 要显示的值1或语句1
when 条件1 then 要显示的值2或语句2
。。。
else 要显示的值n或者语句n
end
##案例:查询员工的工资的情况
如果工资>20000,显示A级别
如果工资>15000,显示B级别
如果工资>10000,显示C级别
否则,显示D级别
select salary, case when salary>20000 then 'A级别' when salary>15000 then 'B级别' when salary>10000 then 'C级别' else 'D级别' end as 工资级别 from employees;
(注意取别名,会好看点,每次end就end了,忘记取别名)
#复习
##常见函数:
字符函数:
length
concat
substr
instr
trim
upper
lower
Lpad
rpad
replace
数学函数:
round
ceil
floor
truncate
mod
日期函数:
now
curdate
curtime
year
month
monthname
day
hour
minute
second
str_to_date
date_format
其它函数:
version
database
user
控制函数
if
case
#题目
##1. 显示系统时间(注:日期+时间)
select now();
##2. 查询员工号,姓名,工资,以及工资提高百分之20%后的结果(new salary)
select employee_id, last_name, salary 旧工资, salary*(1+0.2) as "new salary" from employees;
##3. 将员工的姓名按首字母排序,并写出姓名的长度(length)(substr要先select,否则出不来结果)
select last_name, length(last_name) 姓名长度, substr(last_name,1,1) 首字符 from employees order by 首字符;
(如果按照last_name排序,那么将进行字典排序,首字母一样,第二个字母排,有顺序)
##4. 做一个查询,产生下面的结果(没看懂题目)
select concat(last_name, 'earns', salary, 'monthly but wants', salary*3) as "Dream Salary" from employees where salary = 24000;
##5. 使用case-when,按照下面的条件:(when后要加上’’,then后也要加上’’)(select写完了后要加上,)(case处要写job_id,写别名不行)
job grade
AD_PRES A
ST_MAN B
IT_PROG C
select job_id as job, case job_id when 'AD_PRES' then 'A' when 'ST_MAN' then 'B' when 'IT_PROG' then 'C' end as grade from employees;



