mysql 日期处理成财年财季财月财周

mysql 日期处理成财年财季财月财周,第1张

财年是一个公司的上市时间决定的,所以不同的公司财年码表不同,因为我目前用的财年是从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

1.MySQL获得当前时间戳函数:current_timestamp,current_timestamp()mysql>selectcurrent_timestamp,current_timestamp()+---------------------+---------------------+|current_timestamp|current_timestamp()|+---------------------+---------------------+|2008-08-0923:22:24|2008-08-0923:22:24|+---------------------+---------------------+2.MySQL(Unix时间戳、日期)转换函数:unix_timestamp(),unix_timestamp(date),from_unixtime(unix_timestamp),from_unixtime(unix_timestamp,format)下面是示例:selectunix_timestamp()--1218290027selectunix_timestamp('2008-08-08')--1218124800selectunix_timestamp('2008-08-0812:30:00')--1218169800selectfrom_unixtime(1218290027)--'2008-08-0921:53:47'selectfrom_unixtime(1218124800)--'2008-08-0800:00:00'selectfrom_unixtime(1218169800)--'2008-08-0812:30:00'selectfrom_unixtime(1218169800,'%Y%D%M%h:%i:%s%x')--'20088thAugust12:30:002008'3.MySQL时间戳(timestamp)转换、增、减函数:timestamp(date)--datetotimestamptimestamp(dt,time)--dt+timetimestampadd(unit,interval,datetime_expr)--timestampdiff(unit,datetime_expr1,datetime_expr2)--请看示例部分:selecttimestamp('2008-08-08')--2008-08-0800:00:00selecttimestamp('2008-08-0808:00:00','01:01:01')--2008-08-0809:01:01selecttimestamp('2008-08-0808:00:00','1001:01:01')--2008-08-1809:01:01selecttimestampadd(day,1,'2008-08-0808:00:00')--2008-08-0908:00:00selectdate_add('2008-08-0808:00:00',interval1day)--2008-08-0908:00:00MySQLtimestampadd()函数类似于date_add()。selecttimestampdiff(year,'2002-05-01','2001-01-01')---1selecttimestampdiff(day,'2002-05-01','2001-01-01')---485selecttimestampdiff(hour,'2008-08-0812:00:00','2008-08-0800:00:00')---12selectdatediff('2008-08-0812:00:00','2008-08-0100:00:00')--7MySQLtimestampdiff()函数就比datediff()功能强多了,datediff()只能计算两个日期(date)之间相差的天数。

1、非要用datetime的话就不能用default而要通过trigger来实现

2、在插入时value用now()函数、或者自行取运行时间

3、字段类型改变为:

datecreated timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,

datemodified timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP

但是其实不可行,因为一个表只能有一个CURRENT_TIMESTAMP,但你有三个字段。

所以当有多个时间可能还是得结合方式2与方式3来综合处理。当然全都用方式2也就不会提出这种问题了。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/7324971.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-04
下一篇 2023-04-04

发表评论

登录后才能评论

评论列表(0条)

保存