2016-02-23 51 views
2

我試圖通過刪除所有PHP循環膨脹來縮短我的代碼。我有以下表結構:在每個行的2個時間戳之間生成周字段

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

我想產生StartDateEndDate之間的所有可能的領域WEEKOFYEAR(),顯示當前年份和所有可能WEEKOFYEAR()的的計分Weight

所以結果表看起來是這樣的:

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 

我知道,有的差異58WEEKOFYEAR()之間StartDateEndDate。該StartDate開始於92015年,則EndDate在結束於14星期。 2015年共有周。

回答

1

每一件事情是可以在MySQL 有一個方法,我們可以找到 像

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

日期之間的differnce通過使用這個查詢,你得到的開始日期和結束日期之間的星期數

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) 
+0

我這樣做,但我得到周的量的兩個日期之間,而不是'了'WEEKOFYEAR'之間的差異StartDate'和'EndDate'。我可以這樣做:'WEEKOFYEAR(EndTermin) - WEEKOFYEAR(StartTermin)',但我只限於一年的差異。 – HitomiTenshi

+1

@HitomiTenshi你能提供一些你想要的例子嗎? –

+0

完成,希望它有幫助 – HitomiTenshi

相關問題