gpt4 book ai didi

mysql - 如何在 mysql month wise 中拆分周之间的两个日期

转载 作者:行者123 更新时间:2023-11-29 03:22:18 26 4
gpt4 key购买 nike

我想根据周数显示 MySQL 中的行数。假设我们通过 1 月开始日期 2017-1-1 和结束日期 2017-1-31 然后总天数是 31。我们必须除以 7 然后它是 4 周和 3 天所以总共 5 周所以我必须这样显示

一月

WeekRow
1
2
3
4
5

2 月

WeekRow
1
2
3
4

我正在尝试应用一个查询,但我做不到。我长期以来一直面临这个问题,但仍然无法解决。

最佳答案

下面的代码正在努力解决您的问题。我匆忙写了这段代码.....稍后我会优化这个......看一看……如果您在理解上有任何问题,请告诉我……

CREATE DEFINER=`root`@`localhost` PROCEDURE `sp_month_days_split`(in in_start_date datetime , in in_end_date datetime)
BEGIN

declare temp_date datetime;
declare temp_date_data int ;

drop temporary table if exists temp_split;
create temporary table temp_split
(
number bigint not null
);

while(in_start_date <= in_end_date)
do
set temp_date = (select date_add(in_start_date,interval 7 day));
set temp_date_data = (select day(date_add(in_start_date,interval 6 day)));

set @temp_last_number = (select number from temp_split order by number desc limit 1);

if(temp_date_data % 7) =0
Then
insert into temp_split(number)
select (temp_date_data/7);



else
insert into temp_split(number)
select @temp_last_number+1;


end if ;


set in_start_date = temp_date ;

end while;

select * from temp_split;
END

调用

call sp_month_days_split('2017-12-01', '2017-12-31');

关于mysql - 如何在 mysql month wise 中拆分周之间的两个日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42246681/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com