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

时间:2016-02-23 10:11:18

标签: mysql

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

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

我想在WEEKOFYEAR()StartDate之间生成所有可能的EndDate字段,显示当前年份并将Weight除以所有可能{{1}的计数}}的

因此结果表看起来像这样:

WEEKOFYEAR()

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

修改

这是我的数据:

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

我想将其处理到此表中:

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 WEEKOFYEAR()之间存在 58 StartDate的差异。 EndDate 2015 第9周开始,StartDate 2016年第14周结束 / strong>即可。 2015年总共 53 周。

1 个答案:

答案 0 :(得分:1)

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)