1. 流程控制函数
1. case
① 使用一:case ... when ... then ... end as ...
case相当与Java中的 Swith
语法:
case 要判断的字段或表达式
when 常量1 then 要显示的值1或语句1;
when 常量2 then 要显示的值2或语句2;
......
else 要显示的值n或语句n;
end
②使用二:case when ... then ... end ... as ...
相当于java中的if..else if..else if..else
语法:
case
when 条件1 then 要显示的值1或语句1
when 条件2 then 要显示的值2或语句2
...
else 要显示的值n或语句n
end
2. 时间函数
1.now():返回当前系统日期+时间
select now();
2.curdate():返回当前系统日期,不包含时间
select curdate();
3.curtime():返回当前时间,不包含日期
select curtime();
4.可以获取指定的部分,年,月,日,小时,分钟,秒
select year(now()) 年;
select year('1998-1-1') 年;
select year(hiredate) 年 from employees;
select month(now()) 月;
select monthname(now()) 月;
5.str_to_date:将日期格式的字符转换成指定格式的日期
STR_TO_DATE('9-13-1999','%m-%d-%Y');----> 1999-09-13
6.date_format:将日期转换成字符
DATE_FORMAT('2018/6/6', '%Y年%m月%d日'); --->2018年06月06日
案例:
str_to_date:
1.select str_to_date('1998-3-2','%Y-%c-%d'); --->1998-03-02
2.查询入职日期为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:
1.select date(now(), '%y年%m月%d日');
2.查询有奖金的员工名和入职日期(xx月/xx日 xx年)
select last_name, date_format(hiredate, '%m月/%d日 %y年')
from employees
where commission_pct is not null;