简体   繁体   English

PHP代码选择列的总和和计数

[英]php code to select sum and count of column

there are multiple column in my table .. 我的桌子上有多列..

     col1   col2  col3     price
     500    700    100       10
     501    700    100       20
     502    700    100       30
     503    700    100       10
      4                      70

I need to get count of col1 and display its sum. 我需要获取col1的计数并显示其总和。

and also display sum of price column... 并显示价格总和栏...

But the main thing is i need to display this in last row....after all data...how can select this and echo in last row... 但是最主要的是我需要在最后一行中显示它。...在所有数据之后...如何选择它并在最后一行中显示回声...

plz help me... 请帮我...

I need to echo exactly as i put the data in above table... 我需要完全按照我将数据放在上表中的方式进行回显...

I need sql query and also need help to echo only sum of this two column in last row only...... 我需要sql查询,也需要帮助仅回显最后一行中这两列的总和……

SELECT *,IFNULL(col1,'SUM'), count(*) as count FROM coupon_entry  WHERE Group By col1 WITH ROLLUP


<?php  if (is_array($data)) { foreach($data as $row){ ?>
        <tr>            

            <td><?php echo htmlspecialchars($row['col1']); ?></td>
            <td><?php echo htmlspecialchars($row['col2']); ?></td>          
            <td><?php echo htmlspecialchars($row['col3']); ?></td>
            <td><?php echo htmlspecialchars($row['price']); ?></td>
        </tr>
        <?php } }?> 

One solution is to calculate the sum in PHP with variables : 一种解决方案是使用变量计算PHP的总和:

<?php  if (is_array($data)) {
$totalCol1 = 0; 
$totalPrice = 0;
foreach($data as $row){ 
$totalCol1 += $row['col1'];
$totalPrice += $row['price'];
?>
        <tr>            

            <td><?php echo htmlspecialchars($row['col1']); ?></td>
            <td><?php echo htmlspecialchars($row['col2']); ?></td>          
            <td><?php echo htmlspecialchars($row['col3']); ?></td>
            <td><?php echo htmlspecialchars($row['price']); ?></td>
        </tr>
        <?php } 
        <tr>
            <td><?php echo $totalCol1;?></td>
            <td></td>          
            <td></td>
            <td><?php echo $totalPrice;?></td>
        </tr>
}?> 

Either you have to do 2 separate queries or else you have to do your calculations in PHP - either one is fairly simple although the PHP solution will probably be slightly (negligibly?) faster. 您要么必须执行2个单独的查询,要么就必须使用PHP进行计算-尽管PHP解决方案的速度可能会稍快(可忽略不计?),但其中一个相当简单。

double-query: 双查询:

$r = query('SELECT *
            FROM yada-yada');
while ($row = fetch($r)) {
    echo "<td>$row[col1]</td>\n";
    echo "<td>$row[col2]</td>\n";
    echo "<td>$row[col3]</td>\n";
    echo "<td>$row[price]</td>\n";
}

$r2 = query('SELECT COUNT(*) as cnt, sum(col1) as sum_col1, 
               sum(price) as sum_price
            FROM yada-yada...');
if ($row = fetch($r2)) {
    echo "<td>$row['cnt']</td><td>$row['sum_col1']</td>...$row['sum_price']...\n";
}

calculate in PHP: 用PHP计算:

$cnt = $sum_col1 = $sum_price = 0;
$r = query('SELECT *
            FROM yada-yada');
while ($row = fetch($r)) {
    echo "<td>$row[col1]</td>\n";
    echo "<td>$row[col2]</td>\n";
    echo "<td>$row[col3]</td>\n";
    echo "<td>$row[price]</td>\n";
    $cnt++;
    $sum_col1 += $row['col1'];
    $sum_price += $row['price'];
}
echo "<td>$cnt</td><td>$sum_col1</td>...$sum_price...\n";

Below is my sql query to get count and sum of column. 下面是我的SQL查询以获取计数和列的总和。

SELECT COUNT(coupon) As Total,SUM(Price) AS TotalPrice FROM coupon_entry 从coupon_entry中选择COUNT(优惠券)作为总计,SUM(价格)作为TotalPrice

And put this in seprate row below table... 并将其放在表格下方的单独行中...

<?php   
foreach($tot as $tota) 
?>
        <tr style="background-color:#33CCFF;">
            <td colspan="2" style="text-align:right; font-weight:bold;">Total Coupon</td>

            <td><?php echo $tota['Total'];?></td>
            <td style="text-align:right; font-weight:bold;">Total Amount in Rs</td> 
            <td><?php echo $tota['TotalPrice'];?></td>
        </tr>           
            <?php }?>   

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

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