#三、日期时间
#now 返回当前系统日期+时间
select now();
#curtime 返回当前时间,不包含日期
select curtime();
#可以获取指定的部分:年、月、日、小时、分钟、秒
select year(now()) 年;
select year('1996-1-1') 年;
select year(hiredate) 年 from employees;
select month(now()) 月;
select monthname(now()) 月;
select day(now()) 日;
#str_to_date 将字符通过指定的格式转换成日期
select str_to_date('1998-3-2','%Y-%c-%d') as out_put;
#查询入职日期为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');
#date_format 将日期转换成字符
select date_format(now(),'%y年%m月%d日') as out_put;
#查询有奖金的员工名和入职日期(xx月/xx日 xx年)
select last_name,date_format(hiredate,'%m月/%日 %y年') 入职日期
from employees
where commission_pct is not null;
select * from employees;
#k四、其他函数(系统函数)
select version();
select database();
select user();
#五、流程控制函数
#1. if函数 if else 的效果
select if(10<5,'大','小');
select last_name,commission_pct,if(commission_pct is null,'没奖金,呵呵','有奖金,嘻嘻') 备注
from employees;
#2.case函数的使用一: switch case 的效果
案例:查询员工的工资,要求
部门号=30,显示的工资为1.1倍
部门号=40,显示的工资为1.2倍
部门号=50,显示的工资为1.5倍
其他部门,显示原工资
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;
#案例:查询员工的工资级别
如果工资大于>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;