2011-11-04 109 views
6
 
id || week_id || user_id || catch_id(0,1,2) 
1 || 2  || 6  || 0 
1 || 3  || 6  || 1 
1 || 4  || 6  || 1 
1 || 5  || 6  || 1 
1 || 6  || 6  || 0 
1 || 7  || 6  || 0 
1 || 8  || 6  || 2 
1 || 9  || 6  || 0 
1 || 10  || 6  || 0 
1 || 11  || 6  || 1 

我需要找出最大連續一週catch = 1和最大連續一週catch = 0爲每個用戶(找到所有)沒有。我希望我明確自己。查找最大的連續記錄

在上述表

連續最大抓= 1用於用戶6是(周3,4,5)

最大連續周捕獲= 0用於用戶6是(周6,7和/或9,10周)

我怎麼去。我可以在純sql中做到這一點。一個PHP解決方案也歡迎

回答

3

這應爲一個SQL解決工作排序。雖然它只會給你一個week_id的catch_id問題。我不知道你的表被稱爲所以我在下面的答案稱之爲consecutive

drop table if exists consecutive; 

create table consecutive 
(id int,week_id int,user_id int,catch_id int); 

insert into consecutive values (1,2,6,0); 
insert into consecutive values (1,3,6,1); 
insert into consecutive values (1,4,6,1); 
insert into consecutive values (1,5,6,1); 
insert into consecutive values (1,6,6,0); 
insert into consecutive values (1,7,6,0); 
insert into consecutive values (1,8,6,2); 
insert into consecutive values (1,9,6,0); 
insert into consecutive values (1,10,6,0); 
insert into consecutive values (1,11,6,1); 

select w,count(*) as max_consecutive_weeks 
from 
(
select 
case when @cur_catch_id != catch_id then @cur_week_id := week_id else @cur_week_id end as w, 
@cur_catch_id := catch_id as catchChange, 
c.* 
from consecutive c 
cross join (select @cur_catch_id := -1,@cur_week_id := -1) t 
where user_id = 6 
order by week_id asc 
) t 
where catch_id = 1 
group by w 
order by max_consecutive_weeks desc,w asc 
limit 1; 

您可以使用相同的查詢,通過改變where catch_id = 1where catch_id = 0以獲得最大的連續week_ids與catch_id = 0。

祝你好運!

+0

查詢不能正常工作。只是運行它,並看到 – aWebDeveloper

+0

道歉。在別名中輸入錯字。我編輯了我的答案。 –

1

如果PHP是好的,我會做它直截了當:

  • 檢索具有捕獲所有項目= X(X爲0或1,這取決於你想計算一下)通過項目week_id
  • 迭代的ASC順序:
    • 檢查是否有week_id
    • 更新的縫隙最大
0

我沒有嘗試過的代碼,但它應該工作;可能需要調整一下。

使用SQL並獲得所有行通過week_id

$currentcatch = ''; 
$run = 0; 
$results = array(); 

while($record) { 
    if ($record['catch_id'] == $currentcatch) { 
     $run++; 
    } else { 
     if (!empty($currentcatch)) { 
     if (empty($results[$currentcatch]) { 
      $results[$currentcatch] = $run; 
     } else { 
      if ($results[$currentcatch] < $run) { 
       $results[$currentcatch] = $run; 
      } 
     } 
     } 

     $run = 1; 
     $currentcatch = $record['catch_id']; 
    } 
} 

print_r($results); 
0

這裏是一個PHP solution.I've測試它在我的燈,它應該工作。

/* 
    steps: 
    1.sort the week_ids 
    2.iterate the sorted week_ids and try to get all possible max numbers of consecutive records 
    3.show the greatest one. 

*/ 
$numstr = array(4,2,5,6,7,1); //sample week_ids,should be fetched from db by catch_id 
sort($numstr); 
$int_max = 1; 
$maxs_array = array(); 
for($i=0;$i<sizeof($numstr);$i++) 
{ 
    $k = $i; 
    while($numstr[$k]) 
    { 
     if($numstr[$k+1] && $numstr[$k+1] == $numstr[$k]+1) //duplicate week_ids not considered yet 
     { 
      $int_max ++; 
     } 
     else 
     { 
      array_push($maxs_array,$int_max); 
      $int_max = 1; 
      continue 2; 
      } 
      $k++; 
    } 
} 
sort($maxs_array); 
echo array_pop($maxs_array); //output 4 
1

編寫一個查詢,並得到一個陣列格式一週=捕撈稱爲數據(關鍵是一週漁獲價值),每$連勝

$streak = array(); 
$WeekId = 0; 
$prev = 0; 
$count = 1; 
foreach ($data as $week => $catch) 
{ 
    if($week == ++$WeekId && $prev == $catch) 
    { 
     $count ++; 
     $WeekId = $week; 
    } 
    else 
    { 
     if($prev !== 0) 
     { 
      $streak[$prev][$count]  = $count; 
     } 
     $WeekId      = $week;  
     $count      = 1; 
     $prev      = $catch; 

    } 
} 
$streak[$prev][$count]  = $count; 

現在計算MAX() [0]$連勝[1]