我想獲得具有連續值的行數的最高計數。例如,如果下面是一個表和我數個連續的「A的總數我會得到5.SQL:獲取具有相同值的連續行數最高的方法?
- 甲
- 乙
- 乙
- 甲
- 甲
- A
- A
- A
- 乙
我試圖找到一種簡潔的方式在SQL做到這一點。我想用PHP,但努力去做,並創建了一個凌亂的解決方案:
$streaksql = "SELECT * FROM `mytable`";
$streaksql = $modx->query($streaksql);
$outcomecounter = 0;
$highestoutcome = 0;
while ($streak = $streaksql->fetch(PDO::FETCH_ASSOC)) {
$outcome = $row['outcome'];
if($outcome == 'A'){
$outcomecounter = $outcomecounter +1;
if($outcomecounter > $highestoutcome){
$highestoutcome = $outcomecounter;
}
}
else {
$outcomecounter = 0;
}
};//WHILE
echo $highestoutcome;
我想知道是否有人知道一個更合適的方法在SQL查詢來做到這一點?
你的意思是_consecutive_,對吧? – moonwave99
哎呀是的,我願意 - 謝謝 – MeltingDog
可能重複的[Mysql計數匹配的連續數字行](http://stackoverflow.com/questions/19541762/mysql-counting-the-consecutive-number-rows-that-match) – moonwave99