我想获得具有连续值的行数的最高计数。例如,如果下面是一个表和我数个连续的“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