php - 数据表中的总和是多少? PHP

标签 php mysql database sum

我有下面的代码,我做了一段时间,但我想在 AREA 的每个段中放置一行及其总数,然后继续其余部分

这是我的代码:

  $resultado = $conexion->query("
SELECT Det.OP
     , Det.ID_Area
     , Det.Cliente
     , de.Desc_Area
     , sum(Det.Cantidad) as CantidadOP
     , sum(Det.Volumen) as VolumenOP
     , sum(Det.PrecioTotal) as PrecioTotalOP
  from Despacho_DetalleEntreAreas as Det
  join  Despacho_DescEntreAreas as de 
    on de.ID_Area = Det.ID_Area  
 where Det.Fecha Between '$FDesde' and '$FHasta'
    Group by Det.OP
     , Det.ID_Area
    order by Det.ID_Area
     , Det.OP asc
");
      while($row=$resultado->fetch_assoc())
        {
           $PrecioGrupo=$PrecioGrupo + $row['PrecioTotalOP']; ?>
          <tr>
            <td><?php echo $row['ID_Area'];?></td>
            <td><?php echo $row['Desc_Area'];?></td>
            <td><?php echo $row['OP'];?></td>
            <td><?php echo $row['Cliente'];?></td>
            <td><?php echo $row['CantidadOP'];?></td>
            <td><?php echo number_format($row['VolumenOP'],4,"." , ",");?></td>
            <td><?php echo "$". "". number_format($row['PrecioTotalOP'],0, " " , ",");?></td>
          </tr>

这是我的 table :

<table class="table table-bordered hover" id="Tabla_OpDespachadas_Areas" cellspacing="0">
            <thead>
              <tr>
                <th>ID_AREAS</th>
                <th>AREAS</th>

                <th>CLIENT</th>
                <th>CANT</th>
                <th>VOL</th>
                <th>PRICE</th>

              </tr>
            </thead>
            <tbody>
              <?php if (isset($_POST['Bto_Procesar'])): ?>
              <?php include("Clases/ProcesoOP.php") ?>
              <?php endif; ?>
            </tbody>

我想要这样的东西。 因此,在一个区域的每个部分的末尾,我想放一行包含总计,将该区域的价格相加即为总计,然后继续其余部分我画了一个表格,因为我想看

 ---------------------------------------------  
|id|area       |CLIENT  |CANT |VOL   |PRICE   |  
|--|-----------|--------|-----|------|--------|  
|1 |area1      |xxx1    |10   |1     |1       |  
|1 |area1      |xxx2    |10   |1     |2       |  
|1 |area1      |xxx3    |20   |2     |3       |
|  |           |        |     |      |6       | 
|--|-----------|--------|-----|------|--------|  
|2 |area2      |xxx1    |40   |1     |5       |  
|2 |area2      |xxx2    |45   |1     |5       |  
|2 |area2      |xxx3    |20   |2     |5       |
|  |           |        |     |      |15      | 

我希望我能很好地解释问候

最佳答案

此 PHP 代码应添加您要查找的小计。

<?php
    $resultado = $conexion->query("SELECT Det.OP,Det.ID_Area,Det.Cliente,de.Desc_Area,
    sum(Det.Cantidad) as CantidadOP,
    sum(Det.Volumen) as VolumenOP,
    sum(Det.PrecioTotal) as PrecioTotalOP
    from Despacho_DetalleEntreAreas as Det
    inner join  Despacho_DescEntreAreas as de on de.ID_Area = Det.ID_Area  where Det.Fecha Between '$FDesde' and '$FHasta'
    Group by Det.OP, Det.ID_Area
    order by Det.ID_Area, Det.OP asc");
    $currArea = '';
    $currTotal = 0;
    while($row=$resultado->fetch_assoc()) {
        $PrecioGrupo = $PrecioGrupo + $row['PrecioTotalOP'];
        if($currArea != $row['ID_Area']) {
            if($currArea != '') {
                echo "      <tr>\r\n";
                echo "          <td colspan='6'>&nbsp;</td>\r\n";
                echo "          <td>$".number_format($currTotal,0, " " , ",")."</td>\r\n";
                echo "      <tr>\r\n";
            }
            $currArea = $row['ID_Area'];
            $currTotal = 0;
        }
        $currTotal += $row['PrecioTotalOP'];
?>
        <tr>
            <td><?php echo $row['ID_Area'];?></td>
            <td><?php echo $row['Desc_Area'];?></td>
            <td><?php echo $row['OP'];?></td>
            <td><?php echo $row['Cliente'];?></td>
            <td><?php echo $row['CantidadOP'];?></td>
            <td><?php echo number_format($row['VolumenOP'],4,"." , ",");?></td>
            <td><?php echo "$". "". number_format($row['PrecioTotalOP'],0, " " , ",");?></td>
        </tr>
<?php
    echo "      <tr>\r\n";
        echo "          <td colspan='6'>&nbsp;</td>\r\n";
        echo "          <td>$". number_format($row['PrecioTotalOP'],0, " " , ",")."</td>\r\n";
        echo "      <tr>\r\n";
        }
    echo "      <tr>\r\n";
    echo "          <td colspan='6'>&nbsp;</td>\r\n";
    echo "          <td>$".number_format($currTotal,0, " " , ",")."</td>\r\n";
    echo "      <tr>\r\n";
?>

关于php - 数据表中的总和是多少? PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51902816/

相关文章:

database - 为什么我可以在可为空的列上创建一个带有 PRIMARY KEY 的表?

php - 从记录中缺少的 php 选择查询执行

mysql - 获取MySQL中某条记录(id)之后的记录

php - 双 mysql_real_escape_string 插入新行字符。如何获得新的换行符?

mysql - 如何在一列中打印多行数据?

mysql - 我可以使用 MySQL 触发器更新刚刚添加的行吗

java - 使用 JUnit 进行数据库单元测试

php - 从数据库字段中提取部分数据并创建多个变量

php mysql 如果查询为空则退出

php - 为什么我总是得到0?