在前后端分离项目中(springboot 和vue),想要查询某个表中,当前月份每一天的数据量,请问该怎么写呢,有哪位朋友有方案,万分感谢(因为每个月的天数不一样还有闰年的时候,所以没办法写死,所以想请问要怎么写比较好呢,真的太感谢了)
SELECT
DATE_ADD(
DATE_ADD(
CURDATE(),
INTERVAL - DAY (CURDATE()) + 2 DAY
),
INTERVAL (
cast(
help_topic_id AS signed INTEGER
) - 1
) DAY
) DAY
FROM
mysql.help_topic
WHERE
help_topic_id < DAY (last_day(CURDATE()))
ORDER BY
help_topic_id
可以获取当月的每一天,利用DATE_ADD和CURDATE函数,和MySQL系统自带的help_topic表自增序列来实现
建议在你的mysql数据库创建一张日期表。然后进行关联查询统计,我这边只有sqlserver版本的 ,可以参考
--1、创建数据表 T_Date
CREATE TABLE [dbo].[T_Date](
[the_date] [int] NOT NULL,
[date_name] [nvarchar](30) NULL,
[the_year] [int] NULL,
[year_name] [nvarchar](30) NULL,
[the_quarter] [int] NULL,
[quarter_name] [nvarchar](30) NULL,
[the_month] [int] NULL,
[month_name] [nvarchar](30) NULL,
[the_week] [int] NULL,
[week_name] [nvarchar](30) NULL,
[week_day] [int] NULL,
[week_day_name] [nvarchar](30) NULL,
CONSTRAINT [PK_T_Date] PRIMARY KEY CLUSTERED
(
[the_date] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
-- 2、创建生成日期的存储过程
GO
/****** Object: StoredProcedure [dbo].[SP_CREATE_TIME_DIMENSION] ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[SP_CREATE_TIME_DIMENSION]
@begin_date nvarchar(50)='2015-01-01' ,
@end_date nvarchar(50)='2030-12-31'
as
/*
SP_CREATE_TIME_DIMENSION: 生成时间维数据
begin_date: 开始时间
end_date:结束时间
*/
declare
@dDate date=convert(date,@begin_date),
@v_the_date varchar(10),
@v_the_year varchar(4),
@v_the_quarter varchar(2),
@v_the_month varchar(10),
@v_the_month2 varchar(2),
@v_the_week varchar(2),
@v_the_day varchar(10),
@v_the_day2 varchar(2),
@v_week_day nvarchar(10),
@adddays int=1;
WHILE (@dDate<=convert(date,@end_date))
begin
set @v_the_date=convert(char(10),@dDate,112);--key值格式为yyyyMMdd
set @v_the_year=DATEPART("YYYY",@dDate);--年份
set @v_the_quarter=DATEPART("QQ",@dDate);--季度
set @v_the_month=DATEPART("MM",@dDate);--月份(字符型)
set @v_the_day=DATEPART("dd",@dDate);--日(字符型)
set @v_the_week=DATEPART("WW",@dDate);--年的第几周
set @v_week_day=DATEPART("DW",@dDate); --星期几
-- 插入数据
insert into T_Date(the_date,date_name,the_year,year_name,the_quarter,quarter_name,the_month,month_name,the_week,week_name,week_day,week_day_name)
values(
@v_the_date,
convert(nvarchar(10),@v_the_year)+'年'+convert(nvarchar(10),@v_the_month)+'月'+convert(nvarchar(10),@v_the_day)+'日',
@v_the_year,
convert(nvarchar(10),@v_the_year)+'年',
@v_the_quarter,
convert(nvarchar(10),@v_the_year)+'年'+convert(nvarchar(10),@v_the_quarter)+'季度',
case when @v_the_month>=10 then
convert(int,(convert(nvarchar(10),@v_the_year)+convert(nvarchar(10),@v_the_month)))
else convert(int,convert(nvarchar(10),@v_the_year)+'0'+convert(nvarchar(10),@v_the_month)) end,
convert(nvarchar(10),@v_the_year)+'年'+convert(nvarchar(10),@v_the_month)+'月',
@v_the_week
,'第'+convert(nvarchar(10),@v_the_week)+'周',
@v_week_day,
case @v_week_day-1
when 1 then '星期一'
when 2 then '星期二'
when 3 then '星期三'
when 4 then '星期四'
when 5 then '星期五'
when 6 then '星期六'
when 0 then '星期日'
else '' end
);
set @dDate=dateadd(day,@adddays,@dDate);
continue
if @dDate=dateadd(day,-1,convert(date,@end_date))
break
end
-- 3、执行存储过程生成数据
GO
DECLARE @return_value int
EXEC @return_value = [dbo].[SP_CREATE_TIME_DIMENSION]
SELECT 'Return Value' = @return_value
GO
如果是我我会查出这个月的数据,然后在代码里面处理第一天到每一天的数据量。这样只需要一个简单SQL,代码里面一个循环就可以了,java8的日期时间api有提供获取某个月第一天,某个月最后一天的方法