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
方法一:select date_format(日期字段,'%Y-%m') as '日期' from 表
方法二:mysql有日期函数 date_format
SELECT date_format(signDate,'%Y-%m') AS MY_YM FROM tablename WHERE 1
方法三:对这个字段使用函数left或者substring
例如:
select left(signDate,7) as date from tablename 或者
select substring(signDate,0,7) as date from tablename
扩展资料:MySQL 截取部分日期
使用 DATE_FORMAT(get_date, '%Y-%m-%d') 函数截取。
其中:get_date 是需要截取的字段名;'%Y-%m-%d' 是截取后的日期格式。
select date_format('1997-10-04 22:23:00','%y %M %b %D %W %a %Y-%m-%d %H:%i:%s %r %T')
结果:97 October Oct 4th Saturday Sat 1997-10-04 22:23:00 10:23:00 PM 22:23:00
get_date = "2006-12-07"
SELECT count(*) FROM t_get_video_temp Where DATE_FORMAT(get_date, '%Y-%m-%d')='2006-12-07'
SELECT count(*) FROM t_get_video_temp Where get_date like '2006%-07%'
财年是一个公司的上市时间决定的,所以不同的公司财年码表不同,因为我目前用的财年是从4月1号开始算的,自己就写了个mysql 的工具函数:
SELECT
-- 自然日
naturalday,
-- 财年
concat('FY', DATE_FORMAT(naturalday,'%y'), DATE_FORMAT(naturalday,'%y')+1) as fiscalyear,
-- 财季
concat('Q', case when QUARTER(naturalday)=4 then 1 ELSE QUARTER(naturalday)-1 END
) as fiscalquarter,
-- 财月
DATE_FORMAT(naturalday,'%m') as fiscalmonth,
-- 财周
timestampdiff(WEEK,date(concat(year(naturalday),'-',elt(quarter(naturalday),1,4,7,10),'-',1)),naturalday) as fiscalweek
from tool_lenovo_day
一下是我写的一个存储过程:
CREATE DEFINER=`root`@`%` PROCEDURE `sp_InitialFiscalYear_data`(`sDate` varchar(20),`eDate` varchar(20))
BEGIN
/*
工具函数:生成时间段内的lenovo的财年、财季、财月、财周数据
做成:白纸黑字
做成日:2020-07-16
入参:
sDate 开始自然日
eDate 结束自然日
业务逻辑:
tool_lenovo_day 财年表
有更新、没有新增财年数据
*/
INSERT INTO tool_lenovo_day SELECT
*
FROM
(
SELECT
naturalday,
concat( 'FY', DATE_FORMAT( naturalday, '%y' ), DATE_FORMAT( naturalday, '%y' )+ 1 ) AS fiscalyear,
concat( 'Q', CASE WHEN QUARTER ( naturalday )= 4 THEN 1 ELSE QUARTER ( naturalday )- 1 END ) AS fiscalquarter,
DATE_FORMAT( naturalday, '%m' ) AS fiscalmonth,
timestampdiff(
WEEK,
date(
concat( YEAR ( naturalday ), '-', elt( QUARTER ( naturalday ), 1, 4, 7, 10 ), '-', 1 )),
naturalday
)+1 AS fiscalweek
FROM
(
SELECT
date_add( date_sub(sDate,interval 1 day), INTERVAL ( cast( help_topic_id AS signed INTEGER ) + 1 ) day ) naturalday
FROM
mysql.help_topic
WHERE
help_topic_id <DATEDIFF(eDate,date_sub(sDate,interval 1 day))
ORDER BY
help_topic_id
) as s_e_day
) AS a
ON DUPLICATE KEY UPDATE fiscalyear = a.fiscalyear,
fiscalquarter = a.fiscalquarter,
fiscalmonth = a.fiscalmonth,
fiscalweek = a.fiscalweek
END
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)