显示工作20年以上的员工 课后作业(今天的前20年,今年前20年,时间是 字符串还是日期函数?)
今天前的20年 日期函数
方法一:select * from 表名 where not NOW() between 雇佣日期 and DATE_ADD(雇佣日期,INTERVAL 20 YEAR);(解释说明:如果当前系统时间没有在雇佣日期和雇佣日期加上20年之间,那么认为他已经工作了20年以上。not的位置可以认为当前系统时间在雇佣日期和雇佣日期加上20年之间的取反操作。)
-- select * from employees where not NOW() between hire_date and DATE_ADD(hire_date,INTERVAL 20 YEAR) limit 10;
方法二:select * from 表名 where 雇佣日期< ‘1999-11-09’;(假设查询当天真实时间为‘2019-11-09’)
-- select * from employees where hire_date < '2022-01-11'limit 10;
方法三:select * from 表名 where now() > DATE_ADD(雇佣日期,INTERVAL 20 YEAR);
-- select * from employees where now() > DATE_ADD(hire_date,INTERVAL 20 YEAR)limit 10;
方法四:
select * from t_emp where TIMESTAMPDIFF(year,雇佣日期,current_date)>=20;
-- select * from employees where TIMESTAMPDIFF(year,hire_date,current_date)>=20 limit 10;
now():系统当天时间(可用select now();返回出来)。
date_add():雇佣日期加上“INTERVAL 20 YEAR”的时间。举例说明:
select date_add(now(),interval 20 year);(now():2019-11-09(系统当天时间);所以返回结果:2039-11-09)。
TIMESTAMPDIFF(year,雇佣日期,current_date):计算时间间隔,返回当前日期减去雇佣日期的年份