如何进行粉煤计算以及如何显示总粉煤量

时间:2017-02-27 06:56:08

标签: php mysql

这里我要PF计算,我们每个月扣除pf,现在我想显示总pf数量。这是我的数据库表结构

id    first_name   pf_amount   pf_month    badge_number

1      Kani         200         01-2017      01

2      Mahesh       250         01-2017      02

3      Kani         200         02-2017      01

4      Mahesh       250         02-2017      02

在我的列表页面中,我想显示有400的badge_number(01)和有500的badge_number(02)。这里,badge_number是唯一的

我这样写了查询,但是我在这里得到了所有数据,如何根据我的要求做了

$check = mysql_query("SELECT * FROM pf_history");
while($row = mysql_fetch_array($check)) {
   echo $row['pf_amount'];
}

2 个答案:

答案 0 :(得分:1)

在查询中使用GROUP BY和SUM():

SELECT first_name, pf_month, SUM(pf_amount) as total FROM pf_history GROUP BY badge_number

<强>代码:

$check = mysqli_query("SELECT first_name, pf_month, SUM(pf_amount) as total FROM pf_history GROUP BY badge_number");
while($row = mysqli_fetch_array($check)) {
  echo $row['total'];
}
group by和sum()的

参考

GROUP BY

SUM()

答案 1 :(得分:0)

您应该使用sum()group by,如下所示:

SELECT first_name, pf_month, badge_number, SUM(pf_amount) as total_pf_amount
FROM pf_history 
GROUP BY badge_number

PHP:

$check = mysql_query("SELECT first_name, pf_month, badge_number, SUM(pf_amount) as total_pf_amount
    FROM pf_history 
    GROUP BY badge_number");
while($row = mysql_fetch_array($check)) {
    echo $row['total_pf_amount'];
}

reference