简体   繁体   English

在PHP中按相似的2个字段分组,并与总计数结果?

[英]Group by Similar 2 fields in php and result with total count?

How to Group two Similar fields in php? 如何在php中将两个相似的字段分组?

I tried with GROUP BY DATE(bill.date) , bill.agent_id but it is not working for me 我尝试使用GROUP BY DATE(bill.date),bill.agent_id,但它对我不起作用

Table Structure 1: http://i.stack.imgur.com/yvBF0.jpg (table name is bill_agents ) 表格结构1: http ://i.stack.imgur.com/yvBF0.jpg(表格名称为bill_agents)
Table Structure 2: http://i.stack.imgur.com/38tKh.jpg (table name is bill ) 表格结构2: http//i.stack.imgur.com/38tKh.jpg (表格名称为bill)

Current Result 当前结果

+---------+----+----+----+-----+----+
|         |  1 |  2 |  3 |   4 |  5 |
+---------+----+----+----+-----+----+
| Agent 1 | 35 |  0 |  0 |   0 |  0 |
| Agent 2 |  0 | 10 |  0 |   0 |  0 |
| Agent 1 |  0 |  0 | 12 |   0 |  0 |
| Agent 3 |  0 |  0 |  0 | 100 |  0 |
| Agent 6 |  0 |  0 |  0 |   9 |  0 |
| Agent 2 |  0 |  0 |  0 |   9 | 14 |
+---------+----+----+----+-----+----+

1,2,3,4,5 .... are the days from date 1,2,3,4,5 ....是从日期算起的天数

But I want To get Like The Following 但我想得到如下

+---------+----+----+----+-----+----+----+
|         |  1 |  2 |  3 |   4 |  5 |total
+---------+----+----+----+-----+----+----+
| Agent 1 | 35 |  0 | 12 |   0 |  0 |47  |
| Agent 2 |  0 | 10 |  0 |   0 | 14 |28  |
| Agent 3 |  0 |  0 |  0 | 100 |  0 |100 |
| Agent 6 |  0 |  0 |  0 |   9 |  0 | 9  |
+---------+----+----+----+-----+----+----+

Php Code pasted below that I am using now . 我现在使用的下面粘贴的PHP代码。

<table width="100%" border="1" cellspacing="4" cellpadding="1">
  <tr>
    <td>&nbsp;</td>
     <?php
for ($i=01; $i<=31; $i++)
  {?>
    <td><?php echo $i; ?></td>
    <?php

  }
?>
    <td>Total</td>
  </tr>
  <?php 

    $query4 = "SELECT bill.agent_id, bill.date, SUM(bill.amount + bill.cheque) AS total, bill_agents.id,bill_agents.name ".
          "FROM bill, bill_agents ".
          "WHERE bill.agent_id = bill_agents.id AND YEAR(date)  = YEAR(CURDATE()) AND MONTH(date) = MONTH(CURDATE()) ". 
          "GROUP BY bill.agent_id , DATE(bill.date)   ".

              // "GROUP BY bill.agent_id , DATE(bill.date)  ".
              "ORDER BY bill.date ASC";

    $result4 = mysql_query($query4) or die('Error, query failed1'); 
    if  (mysql_num_rows($result4)>0){
    mysql_data_seek($result4, 0);   

?>
  <?php $total_1 = 0;  while($row4 = mysql_fetch_array($result4, MYSQL_ASSOC)){?>
  <?php $date =    $row4['date'];

    $var = $date;
    $date = date("d-m-Y", strtotime($var) );
    $date=substr($date, 0, -8); 

    echo $date;

    ?>
  <tr>
    <td><?php echo $row4['name']; ?></td>
    <?php
for ($i=01; $i<=31; $i++)
  {?>
    <td><?php if ($date == $i) echo $row4['total']; ?></td>
    <?php

  }
?>
    <td></td>
  </tr>
  <?php } } ?>
  <tr>
    <td colspan="31"></td>
    <td>Total</td>
    <td></td>
  </tr>
</table>

Because I don't know if I understood your question here is a short sample which could point you in the right direction: 因为我不知道我是否理解您的问题,所以这里有一个简短的示例,可以为您指明正确的方向:

$query4 = "SELECT bill.agent_id, "
        . "       bill.date, "
        . "       SUM(bill.amount + bill.cheque) AS total, "
        . "       bill_agents.id, "
        . "       bill_agents.name "
        . "FROM bill, bill_agents "
        . "WHERE bill.agent_id = bill_agents.id "
        . "AND YEAR(date) = YEAR(CURDATE()) "
        . "AND MONTH(date) = MONTH(CURDATE()) "
        . "GROUP BY bill.agent_id, DATE(bill.date)   "
        . "ORDER BY bill.date ASC";

$result4 = mysql_query($query4) or die('Error, query failed1');
if (mysql_num_rows($result4) > 0) {
    mysql_data_seek($result4, 0);

    $agents = array();
    while (true == ($row4 = mysql_fetch_assoc($result4))) {
        $agents[$row4['agent_id']][$row4['date']] = $row4;
    } 

    var_dump($agents);
}

Take a look at the output and see if you can work further with that $agents variable. 查看输出,看看是否可以进一步使用该$agents变量。

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

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