select curdate();查询当前日期,输出格式“xxxx-xx-xx”,年月日格式;
select curtime();查询当前时分秒;
select now();查询当前日期,包含年月日,时分秒;
select year(now());查询当前年份;
select month(now());查询当前月份;
select day(now());查询当前天数;
select date_add(now(),interval 10 day);查询当前日期10天后的日期;
select datediff('2022-3-28','2021-10-23');查询两个日期之间差值;
-- 查询所有员工入职天数,并倒序排序
select name,datediff(curdate(),entrydate) as 'entrydays' from emp order by entrydays desc;
流程控制函数
select if(true,'ok','error'); -- 返回'ok'
select if(false,'ok','error'); -- 返回'error'
select ifnull('hello','default'); -- 返回'hello'
select ifnull('','default'); -- 返回''
select ifnull(null,'default'); -- 返回'default'
-- case when then else end演示
-- 将字段工作城市中的北京和上海替换位一线城市,其他都为二线城市
select
name,
(case address when '北京' then '一线城市' when '上海' then '一线城市' else '二线城市' end) as '工作城市'
from emp;
-- 查询学生成绩,>=85优秀,>=60及格,其它不及格
select
id,
name,
(case when chinese>=85 then '优秀' when chinese>=60 then '及格' else '不及格' end) as '语文' ,
(case when math>=85 then '优秀' when math>=60 then '及格' else '不及格' end) as '数学' ,
(case when english>=85 then '优秀' when english>=60 then '及格' else '不及格' end) as '英语'
from score;