2011-11-04 13 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 

ho bisogno di scoprire la settimana max consecutivo il catch = 1 e max settimana consecutiva il catch = 0 per ogni utente (per tutti). Spero di essere chiaro.Trova max non di record consecutivi

Nella tabella sopra

max cattura consecutiva = 1 per l'utente 6 è (3,4,5 settimane)

max consecutivo settimane catture = 0 per l'utente 6 è (settimana 6,7 ​​e/o settimana 9,10)

Come faccio? Posso farlo in puro sql. Una soluzione php è anche il benvenuto

risposta

3

Questo dovrebbe funzionare per una soluzione SQL. Anche se ti darà solo un week_id per il catch_id in questione. Non so che cosa il vostro tavolo si chiama così l'ho chiamata consecutive nella risposta qui sotto:

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; 

È possibile utilizzare la stessa query per ottenere max week_ids consecutivi con catch_id = 0 modificando where catch_id = 1-where catch_id = 0.

Buona fortuna!

+0

la tua richiesta non funziona. basta eseguirlo e vedere scuse – aWebDeveloper

+0

. Errore di battitura in un alias. Ho modificato la mia risposta. –

1

Se PHP è ok, lo farei semplice:

  • Recupero di tutti gli elementi che hanno pescato = x (x è 0 o 1, a seconda di cosa si desidera calcolare) in ordine di ASC week_id
  • iterazioni sugli articoli:
    • Controllare se v'è una lacuna in week_id
    • Aggiorna massimo
0

Non ho provato il codice, ma dovrebbe funzionare; forse è necessario un piccolo ritocco.

Utilizzare SQL e ottenere tutte le righe ordinato secondo 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

Ecco una soluzione PHP. L'ho testata sulla mia lampada e dovrebbe funzionare.

/* 
    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

Scrivi una query e ottenere un array chiamato dati della settimana Formati = catch (chiave è la settimana e fermo è il valore)

$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; 

Ora calcolare il massimo() di ciascun $ striscia [0] e $ streak [1]

Problemi correlati