大家好,我花了一个下午的时间让我的sql动态填充chart.js图的日期范围,并决定分享代码以帮助将来的任何人。我写的代码将采取的日期范围,你已经输入和检查两个日期之间的时间,然后根据两个日期之间的时间将给你一个不同的时间跨度为您的图形。
这是完整的查询
SELECT
name AS "Company"
IF( DATEDIFF( FROM_UNIXTIME( [end_date], '%Y-%m-%d' ), FROM_UNIXTIME(
[start_date], '%Y-%m-%d' )) <= 8,
DATE_FORMAT( FROM_UNIXTIME( insert_date, '%Y-%m-%d' ), '%a %D %M'),
IF( DATEDIFF( FROM_UNIXTIME( [end_date], '%Y-%m-%d' ), FROM_UNIXTIME(
[start_date], '%Y-%m-%d' )) BETWEEN 8 AND 62,
CONCAT("WC - ", DATE_FORMAT(DATE_ADD(MAKEDATE(FROM_UNIXTIME( insert_date,
'%Y' ), 1), INTERVAL WEEK(FROM_UNIXTIME( insert_date, '%Y-%m-%d' ))
WEEK), "%d %M %Y")),
IF( DATEDIFF(FROM_UNIXTIME( [end_date], '%Y-%m-%d' ), FROM_UNIXTIME(
[start_date], '%Y-%m-%d' )) > 62,
CONCAT(MONTHNAME(STR_TO_DATE(MONTH(FROM_UNIXTIME( insert_date, '%Y-%m-%d'
)), '%m'))," ",FROM_UNIXTIME( insert_date, '%Y' )),''))) AS labels ,
CONCAT(
"Generated (",COUNT( insert_date ),")"
) AS toolTip
FROM (inserted_cases)
WHERE insert_date BETWEEN [start_date] AND [end_date]
GROUP BY labels
现在,为您做所有计算的位在这里,并将为相当多的其他事情以及工作,但我建议它为chart.js,因为我已经在这里测试了它。
IF( DATEDIFF( FROM_UNIXTIME( [end_date], '%Y-%m-%d' ), FROM_UNIXTIME(
[start_date], '%Y-%m-%d' )) <= 8,
DATE_FORMAT( FROM_UNIXTIME( insert_date, '%Y-%m-%d' ), '%a %D %M'),
IF( DATEDIFF( FROM_UNIXTIME( [end_date], '%Y-%m-%d' ), FROM_UNIXTIME(
[start_date], '%Y-%m-%d' )) BETWEEN 8 AND 62,
CONCAT("WC - ", DATE_FORMAT(DATE_ADD(MAKEDATE(FROM_UNIXTIME(
insert_date, '%Y' ), 1), INTERVAL WEEK(FROM_UNIXTIME(
insert_date, '%Y-%m-%d' )) WEEK), "%d %M %Y")),
IF( DATEDIFF(FROM_UNIXTIME( [end_date], '%Y-%m-%d' ), FROM_UNIXTIME(
[start_date], '%Y-%m-%d' )) > 62,
CONCAT(MONTHNAME(STR_TO_DATE(MONTH(FROM_UNIXTIME( insert_date, '%Y-
%m-%d' )), '%m'))," ",FROM_UNIXTIME( insert_date, '%Y' )),''))) AS
labels ,
我希望这能有助于节省这么多时间在未来。另外,如果你知道更好的方法,我总是乐于接受建议和学习新事物。快乐的家伙们。:)
暂无答案!
目前还没有任何答案,快来回答吧!