gpt4 book ai didi

mysql - 为每行生成 2 个时间戳之间的周字段

转载 作者:行者123 更新时间:2023-11-29 02:51:09 25 4
gpt4 key购买 nike

我试图通过删除所有 PHP 循环膨胀来缩短我的代码。我有以下表结构:

tdata_weight
-----
INT id (PK)
VARCHAR Name
DOUBLE Weight
TIMESTAMP StartDate
TIMESTAMP EndDate

我想在 StartDateEndDate 之间生成所有可能的 WEEKOFYEAR() 字段,显示当前年份并划分 Weight 由所有可能的 WEEKOFYEAR() 的计数决定。

所以结果表看起来像这样:

tdata_weight_part
-----
VARCHAR Name
DOUBLE WeightPart
INT Year
INT Week

我当前的解决方案涉及在 PHP 中循环遍历表,但我想通过 SQL 查询完成大部分工作来缩短它...如果可能的话。

编辑:

这是我的数据:

id: 1
Name: 'Machine1'
Weight: 50000
StartDate: 2015-03-01 00:00:00
EndDate: 2016-04-09 00:00:00

我想把它处理成这张表:

Name: 'Machine1'
WeightPart: 862.07
Year: 2015
Week: 9

Name: 'Machine1'
WeightPart: 862.07
Year: 2015
Week: 10

Name: 'Machine1'
WeightPart: 862.07
Year: 2015
Week: 11

(...)

Name: 'Machine1'
WeightPart: 862.07
Year: 2016
Week: 14

我知道 StartDateEndDate 之间有 58 WEEKOFYEAR() 的差异。 StartDate 开始于 2015 年第 9 周EndDate 结束于第 14 周 2016。 2015 年共有 53 周。

最佳答案

mysql 一切皆有可能有一种方法可以找到日期之间的差异喜欢

SELECT TIMESTAMPDIFF(WEEK,'2016-02-01','2016-02-29');

通过使用此查询,您可以获得开始日期和结束日期之间的周数

SELECT TIMESTAMPDIFF(WEEK,StartDate,EndDate);

编辑

set @a = 0;
set @timeDiffence := TIMESTAMPDIFF(WEEK, '2015-03-01 00:00:00', '2016-04-09 00:00:00');
set @start_week_of_year := WEEKOFYEAR('2015-03-01');
set @start_year := YEAR('2015-03-01');
SELECT weeks, years FROM (
select @start_week_of_year AS weeks, @start_year AS years,IF(@start_week_of_year = 52, @start_year := @start_year + 1,'' ),
IF(@start_week_of_year < 52, @start_week_of_year := @start_week_of_year +1, @start_week_of_year := 1),
if(@a < @timeDiffence, @a := @a+1, @a := null) from
(select adddate('1970-01-01',t4.i*10000 + t3.i*1000 + t2.i*100 + t1.i*10 + t0.i) selected_date from
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t1,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t3,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t4) v
where selected_date between '2015-03-01' and '2016-04-09' and @a is not null) AS TEMP;

这将为您提供两个日期之间的周和年输出

+-------+-------+
| weeks | years |
+-------+-------+
| 9 | 2015 |
| 10 | 2015 |
| 11 | 2015 |
| 12 | 2015 |
| 13 | 2015 |
| 14 | 2015 |
| 15 | 2015 |
| 16 | 2015 |
| 17 | 2015 |
| 18 | 2015 |
| 19 | 2015 |
| 20 | 2015 |
| 21 | 2015 |
| 22 | 2015 |
| 23 | 2015 |
| 24 | 2015 |
| 25 | 2015 |
| 26 | 2015 |
| 27 | 2015 |
| 28 | 2015 |
| 29 | 2015 |
| 30 | 2015 |
| 31 | 2015 |
| 32 | 2015 |
| 33 | 2015 |
| 34 | 2015 |
| 35 | 2015 |
| 36 | 2015 |
| 37 | 2015 |
| 38 | 2015 |
| 39 | 2015 |
| 40 | 2015 |
| 41 | 2015 |
| 42 | 2015 |
| 43 | 2015 |
| 44 | 2015 |
| 45 | 2015 |
| 46 | 2015 |
| 47 | 2015 |
| 48 | 2015 |
| 49 | 2015 |
| 50 | 2015 |
| 51 | 2015 |
| 52 | 2015 |
| 1 | 2016 |
| 2 | 2016 |
| 3 | 2016 |
| 4 | 2016 |
| 5 | 2016 |
| 6 | 2016 |
| 7 | 2016 |
| 8 | 2016 |
| 9 | 2016 |
| 10 | 2016 |
| 11 | 2016 |
| 12 | 2016 |
| 13 | 2016 |
| 14 | 2016 |
+-------+-------+
58 rows in set (0.09 sec)

关于mysql - 为每行生成 2 个时间戳之间的周字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35574549/

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