简答方法直接用datediff函数
select DATEDIFF('2016-01-01','2016-12-31')
2. 复杂方法
#建立存储过程
drop procedure if exists test
create procedure test(year varchar(50))
BEGIN
set @sql=concat("select datediff('",year,"-12-31','",year,"-01-01')")
prepare execsql from @sql
execute execsql
end
#执行
call test('2016')
我假定你的入职日期字段格式为date,并且字段名为 t_date,那么下面这条语句可能可以满足你的要求.select * from( SELECT year(now())-year(t_date) as t_year FROM `test_t` where year(t_date) >1990 ) as tmp_data where t_year >10
====================
说明一下思路:
子查询计算出入职的年数,并将结果添加字段别名t_year,这个语句只select了t_date字段,其他字段请按需要添加.
SELECT year(now())-year(t_date) as t_year FROM `test_t` where year(t_date) >1990
然后使用另一条查询来得到大于10年的职工.
select * from (子查询语句) as temp_data where t_year>10
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)