我有一条记录,其中包含每天捕获的customerId,GroupId,amountContributed。现在我想返回一个表格,显示一个客户使用组ID提供的总金额。我之前的代码是:
if(isset($_POST["searchVal"])){
//$searchText = $_POST["searchText"];
$sDate = $_POST["sDate"];
$eDate = $_POST["eDate"];
$query = mysqli_query($connection,"SELECT custid,grpid,amountContribute FROM tab_customer_dailycontribution WHERE cast(transactionDate as date) BETWEEN '$sDate' AND '$eDate'");
if($connection->affected_rows>0){
$c =0;$con =0.00;
while($rw = mysqli_fetch_array($query)){
$c++;?>
<tr>
<td><?php echo $c;?></td>
<td><?php echo $rw['custid'];?></td>
<td><?php echo $rw['grpid'];?></td>
<?php $con+=$rw['amountContribute']; ?>
<td><?php echo number_format($con,2);?></td>
<?php } ;?>
</tr>
<tr>
上面给出了这个表:
S/N CustomerID Group ID Total Contribution
1 SC20151 SGC20151 700.00
2 SC20151 SGC20151 500.00
3 SC20151 SGC20151 500.00
4 SC20152 SGC20151 500.00
5 SC20152 SGC20151 500.00
6 SC20152 SGC20151 500.00
7 SC20152 SGC20151 500.00
8 SC20152 SGC20151 500.00
9 SC20152 SGC20151 500.00
10 SC20152 SGC20151 500.00
11 SC20152 SGC20151 500.00
12 SC20152 SGC20151 500.00
13 SC20152 SGC20151 500.00
14 SC20153 SGC20152 1,000.00
15 SC20153 SGC20152 1,000.00
所以,我希望输出如下表所示:
SN Customer ID Group ID Amount Conributed
1 SC20152 SCG20151 5000
2 SC20151 SCG20151 1200
3 SC20153 SCG20152 2000
有人可以提供帮助。我使用groupid by customerid,amountcontributed但不工作。
答案 0 :(得分:1)
SELECT custid, grpid, SUM(amountContribute) AS sumAmountContribute
FROM tab_customer_dailycontribution
WHERE CAST(transactionDate AS date) BETWEEN '$sDate' AND '$eDate'"
GROUP BY custid, grpid
会为您提供所需的信息。
输出时,您必须更改数组索引:
<?php echo $rw['sumAmountContribute']; ?>