2012-09-12 11 views
5

devo struttura della tabella come segueottenere i record consecutivi in ​​MySQL

ID   user_id   win 
1    1    1 
2    1    1 
3    1    0 
4    1    1 
5    2    1 
6    2    0 
7    2    0 
8    2    1 
9    1    0 
10    1    1 
11    1    1 
12    1    1 
13    1    1 
14    3    1 

mi vuole ottenere vittorie consecutive (WIN = 1) per ogni utente in mysql.like per user_id = 1, deve restituire 4 (record di id 10,11,12,13), per user_id = 2 (id del record = 5), dovrebbe restituire 1.

Posso farlo in php dopo il record di registrazione per ciascun utente ma non so come può farlo usando query su mysql.

Anche ciò che sarebbe meglio in termini di prestazioni, utilizzando php o mysql. Qualsiasi aiuto sarà apprezzato. Grazie !!!

+0

perché no 1, 2,4,9? –

+0

no, poiché 1,2,4,9 non è consecutivo. L'user_id = 1 perde in record_id = 3 –

+2

ben 1,2 sono vittorie consecutive, vuoi la striscia vincente più lunga? –

risposta

1

Non sono sicuro se sei riuscito a ottenere l'altra query per lavorare, ma qui è il mio tentativo, sicuramente lavorando - Sqlfiddle per dimostrarlo.

set @x=null; 
set @y=0; 

select sub.user_id as user_id,max(sub.streak) as streak 
from 
(
select 
case when @x is null then @x:=user_id end, 
case 
when win=1 and @x=user_id then @y:[email protected]+1 
when win=0 and @x=user_id then @y:=0 
when win=1 and @x<>user_id then @y:=1 
when win=0 and @x<>user_id then @y:=0 
end as streak, 
@x:=user_id as user_id 
from your_table 
) as sub 
group by sub.user_id 

Come farlo funzionare su una pagina PHP e test per vedere che stai ricevendo i giusti risultati è sotto, ho anche ottimizzato la query un po ':

mysql_query("set @y=0"); 
$query=mysql_query("select sub.user_id as user_id,max(sub.streak) as streak 
from 
(
select 
case 
when win=1 and @x=user_id then @y:[email protected]+1 
when win=0 and @x=user_id then @y:=0 
when win=1 and @x<>user_id then @y:=1 
when win=0 and @x<>user_id then @y:=0 
end as streak, 
@x:=user_id as user_id 
from your_table 
) as sub 
group by sub.user_id"); 
while($row=mysql_fetch_assoc($query)){ 
print_r($row);} 
+0

grazie !! Grazie mille :) –

+0

ciao, l'esecuzione di questa query utilizzando la funzione mysql_query in php non restituisce nulla, mentre questo funziona bene su phpmyadmin. Qualsiasi suggerimento per questo.Grazie –

+0

@PriteshGupta Ciao, ho modificato il mio post per includere come ho fatto a lavorare su una pagina PHP che ho fatto. – mrmryb

4

Query interna conta ciascuna serie. La query esterna ottiene il massimo per utente. Query non è testato (ma sulla base di quello che funziona)

set @user_id = null; 
set @streak = 1; 

select user_id, max(streak) from (
    SELECT user_id, streak, 
    case when @user_id is null OR @user_id != user_id then @streak := 1 else @streak := @streak + 1 end as streak_formula, 
    @user_id := user_id, 
    @streak as streak 
    FROM my_table 
) foo 
group by user_id 
+0

lo sto testando. ti farò sapere presto –

+0

Non riesco a eseguirlo per mysql. Potresti gentilmente spiegarlo così posso correggere le cose che sto facendo male. Avevo sostituito my_table con il mio nome da tavolo. è un'altra cosa deve essere cambiata? –

+0

Che errore ottieni? My_table contiene una colonna denominata user_id? –

Problemi correlati