php - 如何使用 PHP 对 MySQL 表中的每一列求和

标签 php mysql

我想显示表格内每列的总计。这是我的代码。

$Tab_info['Width'] = "600" ;
$Col_det = array(
    'Name' => array( 'Name',
                    'Aging Create Order ( <= 5 )',
                    'Aging Create Order ( > 5 )',
                    'Total'),
    'Size' => array( '200' ,  '200' ,  '200' ,  '100'  ),
    'Type' => array('sortable-text fd-column-', 'sortable-numeric fd-column-','sortable-numeric fd-column-','sortable-numeric fd-column-'));

$Delim_String = '<table id="Summary" class="sortable-onload-0-normal rowstyle-alt no-arrow" style="width:'.$Tab_info['Width'].'px;background-color:#FFFFFF;">';
$Delim_String .= '<thead><tr>'; 
                    for ($Col_count = 0; $Col_count < sizeof($Col_det['Name']) ; $Col_count++) {
$Delim_String .= '<th style="-moz-user-select: none;" class="'.$Col_det['Type'][$Col_count].$Col_count.'" width="'.$Col_det['Size'][$Col_count].'">';
$Delim_String .= '<a title="Sort on '.$Col_det['Name'][$Col_count].'" href="#">'.$Col_det['Name'][$Col_count].'</a>';
$Delim_String .= '</th>';
}

$Delim_String .= '</tr>
  </thead>
  <tbody>
    '; 
//===================Summary Aging Create Order End=========================== 

$vSQL = "SELECT u.Nama as Name,
         SUM( CASE WHEN t.`AgingCreateOrder` <= 5 THEN 1 ELSE 0 END) 'Aging Less Than or Equal 5', 
         SUM( CASE WHEN t.`AgingCreateOrder` > 5 THEN 1 ELSE 0 END) 'Aging More Than 5',
         COUNT(*) as Total
            FROM `oct_usergroup` u
                JOIN `oct_tickets` t 
                ON (t.`Creator_SubType`= u.`Id`)
            WHERE
                t.Date_Created >= (CURDATE() -INTERVAL 49 DAY) 
                AND u.Nama IS NOT NULL
                AND t.AgingCreateOrder between '1' and '1999'
                AND t.Status = 1
            GROUP BY u.`Nama`";                                 

$result = mysql_query($vSQL)or die($vSQL."<br/><br/>".mysql_error());
while($row = mysql_fetch_array ($result))
    {           
$Delim_String .= '
        <tr>
            <td>'.$row['Name'].'</td>
            <td>'.$row['Aging Less Than or Equal 5'].'</td>
            <td>'.$row['Aging More Than 5'].'</td>
            <td>'.$row['Total'].'</td>
        </tr>';
    }

$Delim_String .= "</tbody></table>";
$table1 = $Delim_String;
unset($Delim_String);

//===================Summary Aging Update Address start===========================
$Tab_info['Width'] = "600" ;
$Col_det = array(
    'Name' => array( 'Name',
                    'Aging Update Address ( <= 5 )',
                    'Aging Update Address ( > 5 )',
                    'Total'),
    'Size' => array( '200' ,  '200' ,  '200' ,  '100'  ),
    'Type' => array('sortable-text fd-column-', 'sortable-numeric fd-column-','sortable-numeric fd-column-','sortable-numeric fd-column-')
    );

$Delim_String = '<table id="Summary" class="sortable-onload-0-normal rowstyle-alt no-arrow" style="width:'.$Tab_info['Width'].'px;background-color:#FFFFFF;">
';
$Delim_String .= '<thead>
    <tr>
    '; 
for ($Col_count = 0; $Col_count < sizeof($Col_det['Name']) ; $Col_count++) {
$Delim_String .= '
<th style="-moz-user-select: none;" class="'.$Col_det['Type'][$Col_count].$Col_count.'" width="'.$Col_det['Size'][$Col_count].'">';
$Delim_String .= '<a title="Sort on '.$Col_det['Name'][$Col_count].'" href="#">'.$Col_det['Name'][$Col_count].'</a>';
$Delim_String .= '</th>
';
}

$Delim_String .= '</tr>
  </thead>
  <tbody>
    ';

我希望它像这样显示。

====================================================================
| Name     | Aging Create Order <=5 | Aging Create Order >5 | Total |
---------------------------------------------------------------------
| Consumer | 159                    | 80                    | 239   |
---------------------------------------------------------------------
| CSO      | 20                     | 50                    | 70    |
---------------------------------------------------------------------
| Total    | 179                    | 130                   | 309   |
=====================================================================

最后一行是要添加的行。

最佳答案

试试这个

$result = mysql_query($vSQL)or die($vSQL."<br/><br/>".mysql_error());

$gt_less = 0;
$gt_more = 0;
$gt_total = 0;
while($row = mysql_fetch_array ($result))
{  
    $gt_less += $row['Aging Less Than or Equal 5']; 
    $gt_more += $row['Aging More Than 5'];
    $gt_total += $row['Total'];       
    $Delim_String .= '
    <tr>
        <td>'.$row['Name'].'</td>
        <td>'.$row['Aging Less Than or Equal 5'].'</td>
        <td>'.$row['Aging More Than 5'].'</td>
        <td>'.$row['Total'].'</td>
    </tr>';
 }
 $Delim_String .= '
    <tr>
        <td>Grand Total</td>
        <td>'.$gt_less.'</td>
        <td>'.$gt_more.'</td>
        <td>'.$gt_total.'</td>
    </tr>';

$Delim_String .= "</tbody></table>";

关于php - 如何使用 PHP 对 MySQL 表中的每一列求和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30339443/

相关文章:

javascript - 在 JavaScript 的 IF 语句中使用 PHP

mysql - 为什么有不同的 TEXT 类型?

mysql - Multi-Tenancy : Hibernate with MySQL

HDD上的Mysql表大小

mysql - 如果我有重复的 'WHERE' 字段,如何更新 MySQL 条目?

php - 使用 Cakephp 选择 id 不在另一个表中的所有行

php - Symfony 3 - 将 Controller 代码外包到服务层

php - 在 WooCommerce Checkout 自定义文本字段中启用日期选择器

python - Django:count() 的替代品

php - 使用 axios 的 API 请求总是未经 Laravel API 授权