21,873
社区成员




$link = mysql_connect('localhost','root','123456');
if (!$link) {
die('Could not connect to MySQL: ' . mysql_error());
}
mysql_select_db("test");
mysql_query("SET NAMES utf8");
$sql="SELECT count(f1) as count,group_concat(id) as ids FROM test WHERE f1=1 group by f1";
$res=mysql_query($sql,$link);
while($row=mysql_fetch_row($res)){
$data=$row;
}
$ids=explode(',',$data[1]);
sort($ids);
$id=$f=0;
foreach($ids as $k=>$v){
if($id==$v){
$tmp[]=$v;
$id++;
$f++;
}else{
$tmp=array();
$tmp[]=$v;
$f=0;
$id=++$v;
$f++;
}
if($f>=4){
break;
}
}
if($f>=4){
echo '连续出现4次,它们的id是:';
echo "<pre>";
print_r($tmp);
echo "</pre>";
}else{
echo '没有连续出现4次';
}
mysql_close($link);
function maxSerial($arr){
$max = 0;
$serial = 0;
$count = sizeof($arr);
for($i =0; $i < $count; ++$i){
if($arr[$i] == 1)
++$serial;
else if($serial > $max){
$max = $serial;
$serial = 0;
}
}
return $serial > $max ? $serial : $max;
}