简体   繁体   English

如何按月将每行的总行数和用户名相加

[英]How to sum total row each month group by month and user name

I have a sales record table for every month. 我每个月都有一张销售记录表。 Every salesman have their own code bd and nk in this case. 在这种情况下,每个推销员都有自己的代码bdnk

MySQL MySQL的

select sale_plc
      , month(sale_date) as mon
      , sum(sale_cost) as cost 
from daily_sales 
 where year(sale_date)='2016' 
group 
    by year(sale_date)
      , month(sale_date)
      , sale_plc 
order 
    by month(sale_date)
      , sale_plc asc 
 LIMIT 0, 30

After the query I've got this result: 查询后我得到了这个结果:

id  mon cost
bd  1   224787
nk  1   721102
bd  2   440399
nk  2   898020
bd  3   363543
nk  3   878250

While

  • id =salesman code(sale_plc) id =销售员代码(sale_plc)
  • mon =month number 星期一 =月号
  • cost =total sale of this month from this ID. 成本 =此ID的本月总销售额。

According to the result. 根据结果​​。 I expect the result with array like this. 我希望结果像这样的数组。 Array 排列

(
    [1] => Array
        (
            [mon] => 1
            [bd] => 224787
            [nk] => 721102
        )
    [2] => Array
        (
            [mon] => 2
            [bd] => 440399
            [nk] => 898020
        )
    [3] => Array
        (
            [mon] => 3
            [bd] => 363543
            [nk] => 878250
        )
    )

I know that it is something to do with the sale_plc . 我知道这与sale_plc I need to make it array but I don't have an idea to do so. 我需要制作数组,但我不知道这样做。

I don't have you SQL output, so i create an array and make my own code with the same functionality as i do for your array. 我没有SQL输出,所以我创建一个数组并使用与我为您的数组相同的功能创建自己的代码。

PHP PHP

$arr = array(array("id" => "bd", "mon" => "1", "cost" => "224787"),
            array("id" => "nk", "mon" => "1", "cost" => "721102"),
            array("id" => "bd", "mon" => "2", "cost" => "440399"),
            array("id" => "nk", "mon" => "2", "cost" => "898020"),
            array("id" => "bd", "mon" => "3", "cost" => "363543"),
            array("id" => "nk", "mon" => "3", "cost" => "878250"),
            );

$output_arr = array();      
$tmp = 0;
foreach($arr as $key => $value){
    if($tmp == 0 || $tmp != $value['mon'])
        $output_arr[$value['mon']][mon] = $value['mon'];
    if($value['id'] == 'bd')
        $output_arr[$value['mon']][$value['id']] = $value['cost'];
    if($value['id'] == 'nk')
        $output_arr[$value['mon']][$value['id']] = $value['cost'];
    $tmp = $value['mon'];
}
echo "<pre>";
print_r($output_arr);
echo "</pre>";

Output 产量

Array
(
    [1] => Array
        (
            [mon] => 1
            [bd] => 224787
            [nk] => 721102
        )

    [2] => Array
        (
            [mon] => 2
            [bd] => 440399
            [nk] => 898020
        )

    [3] => Array
        (
            [mon] => 3
            [bd] => 363543
            [nk] => 878250
        )

)

I have already answer With your SQL Output. 我已经回答了你的SQL输出。 Given the example with your SQL. 给出SQL的示例。

PHP PHP

$output_arr = array();
//Your sql
$sql = "select sale_plc
      , month(sale_date) as mon
      , sum(sale_cost) as cost 
from daily_sales 
 where year(sale_date)='2016' 
group 
    by year(sale_date)
      , month(sale_date)
      , sale_plc 
order 
    by month(sale_date)
      , sale_plc asc 
 LIMIT 0, 30";
$tmp = 0;
$i = 0;
$qry = mysqli_query($conn, $sql);
while ($obj = mysqli_fetch_object($qry )){
    if($tmp == 0 || $tmp != $obj->mon)
        $output_arr[$obj->mon][mon] = $obj->mon;
    if($obj->id == 'bd')
        $output_arr[$obj->mon][$obj->id] = $obj->cost;
    if($obj->id == 'nk')
        $output_arr[$obj->mon][$obj->id] = $obj->cost;
    $tmp = $obj->mon;
}

print_r($output_arr);

Output: 输出:

Array(
[1] => Array
    (
        [mon] => 1
        [bd] => 224787
        [nk] => 721102
    )
[2] => Array
    (
        [mon] => 2
        [bd] => 440399
        [nk] => 898020
    )
[3] => Array
    (
        [mon] => 3
        [bd] => 363543
        [nk] => 878250
    )
)

Try this answer, if any problem then please let me know. 试试这个答案,如果有任何问题,请告诉我。

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

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