Php mysql将总和(列)分组为最大值,但总和必须低于给定值

Php mysql将总和(列)分组为最大值,但总和必须低于给定值,php,mysql,Php,Mysql,我有一个带有列名称和金额的表,我想查询每个组中总和(金额)高达1000的数据,如果行值大于1000,它仍然可以显示在自己的组中。我曾经尝试过使用一个php循环,循环的总和在500到1000之间,但是这种方法产生了太多的组,我希望这些组是无效的。有没有任何方法可以在一个mysql查询中实现这一点。问候 我就是这样歪道的 //this query helped me in maneuvering in provision of limits each time $totalLines=mysql_

我有一个带有列名称和金额的表,我想查询每个组中总和(金额)高达1000的数据,如果行值大于1000,它仍然可以显示在自己的组中。我曾经尝试过使用一个php循环,循环的总和在500到1000之间,但是这种方法产生了太多的组,我希望这些组是无效的。有没有任何方法可以在一个mysql查询中实现这一点。问候

我就是这样歪道的

//this query helped me in maneuvering in provision of limits each time 
$totalLines=mysql_query("select COUNT(*) as totalRows from chasis where chasis.BL_No =xxxx order by chasis.BL_No ")or die(mysql_error());   
$fetch_totalLines=mysql_fetch_array($totalLines);
$found_rows=$fetch_totalLines['totalRows'];
$total=0;
$Toplimit=0;
$z=0;
a:
if($Toplimit>0){
$Toplimit=$Toplimit;
//$found_rows=$found_rows-$Toplimit;
$total=0;
}


$query=mysql_query("select * from chasis where chasis.BL_No =xxxx order by chasis.duty_amount limit ".$Toplimit.",".$found_rows." ")or die(mysql_error());  
$i=1;
while($row=mysql_fetch_array($query)){
$total=round($row['duty_amount'],0)+$total;
 echo'<tr>
<td>'.$i.'</td>
<td>'.$row['BL_No'].'</td>
<td>'. mb_substr($row["make"], 0, 1,"UTF8")."-";
            echo $row["model"];  
            echo'</td>
<td>'.$row['chasisNo'].'</td>
<td>'.$row['cc'].'</td>
<td>'.date("Y F", strtotime($row['year'])).'</td>
<td>'.$row['EntryNumber'].'</td>
<td>'.round($row['duty_amount'],0).'</td>
<td>&nbsp;</td>
</tr>';
if($total<=1000 and $total>=500 ){

  echo"<td colspan='8' align='right'><b>Duty Cheque for ".$i." units</b></td>
  <td><b>".$total."</b></td>
  ";
  $Toplimit=$Toplimit+$i;
  $z=$z+$i;
   goto a;
  }
  $i++;

 }
//这个查询每次都帮助我灵活地提供限制
$totalines=mysql\u query(“选择COUNT(*)作为chasis中的totalRows,其中chasis.BL\u No=xxxx order by chasis.BL\u No”)或die(mysql\u error());
$fetch\u totalines=mysql\u fetch\u数组($totalines);
$found_rows=$fetch_totalLines['totalRows'];
$total=0;
$Toplimit=0;
$z=0;
a:
如果($Toplimit>0){
$Toplimit=$Toplimit;
//$found_rows=$found_rows-$Toplimit;
$total=0;
}
$query=mysql\u query(“从chasis中选择*,其中chasis.BL\u No=xxxx订单按chasis.duty\u金额限制“$Toplimit.”、“$found\u rows.”)或die(mysql\u error());
$i=1;
while($row=mysql\u fetch\u array($query)){
$total=四舍五入($row[‘关税金额’],0)+$total;
回声'
“.$i.”
“.$行['BL_编号]。”
“.mb_substr($row[“make”]、0、1、“UTF8”)。”-“;
echo$行[“模型”];
回声'
“.$row['chasisNo']”
“.$row['cc']”
“.日期(“Y F”,标准时间($row['year'])”
“.$row['EntryNumber']”
“.四舍五入($row['duty\u amount',0)。”
';
如果($total=500){
echo“i美元单位的关税支票”
“$总计。”
";
$Toplimit=$Toplimit+$i;
$z=$z+$i;
转到a;
}
$i++;
}
我决定展示我所做的一切,以反映goTo语句用于保存列id的用法
我知道这是一个糟糕的处理方法,但我真的被卡住了。

你能试试这种方法吗

select * from chasis where chasis.BL_No =xxxx AND chasis.duty_amount>=500 AND chasis.duty_amount<=1000 order by chasis.duty_amount

从chasis中选择*,其中chasis.BL\u No=xxxx和chasis.duty\u amount>=500和chasis.duty\u amountShaymol,实际上我想要的是分组,分组的总税额不超过1000,但与之接近,而500正是我一开始试图处理它的方式。你能发布一些你想要的样本数据和输出吗?这是我设法制作的,但问题是有太多的组可以避免