繁体   English   中英

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

[英]mysql group the sum(column) to maximum values but the sums must be below a given value

我有一个带有列名称和数量的表,我想查询总和(总计)最多为1000的数据,如果行值大于1000,它仍可以显示在自己的组中。 我已经尝试使用范围在500到1000之间的php循环求和,但是这种方式产生了很多我希望可以作废的组。 有什么方法可以在单个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++;

 }

我已经决定显示我所做的所有事情,以反映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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM