如何按年份将数据分为带薪值和无薪值

时间:2019-08-11 22:55:31

标签: javascript php mysql sql morris.js

我有一个MYSQL数据库,我想在该数据库中对所有未付金额和未付金额进行排序。我使用的查询是:

SELECT DISTINCT 
YEAR( app_payments.created_at ) AS YEARS,
SUM( app_payments.amount ) AS Total,
app_users.serial,
app_payments.`status` AS payment_state 
FROM
app_payments
INNER JOIN app_users ON app_payments.created_by = app_users.serial 
WHERE
app_payments.created_by = 'd88faa' 
GROUP BY
YEAR ( app_payments.created_at ),
app_payments.status 

我得到的结果是:

2017    1995    d88faa  1
2018    1200    d88faa  1
2019    1250    d88faa  0
2019    4990    d88faa  1

1代表PAID0代表UNPAID

在我的php代码中,我试图将数据分组为年份

$Stats = array ();
while(!$this->EndofSeek()){
$result = $this->Row();
if($result->payment_state == 0 ){
 if(in_array($result->YEARS,$Stats)){
 array_replace($Stats,['y'=>$result->YEARS , 'b'=>$result->Total ]);
}else{ array_push($Stats,['y'=>$result->YEARS , 'a'=>0 , 'b'=>$result->Total ]);}
}else if($result->payment_state == 1){
 array_push($Stats,['y'=>$result->YEARS , 'a'=>$result->Total , 'b'=>0 ]);
}
 }
  return json_encode($Stats)

这将返回输出:

[{"y":"2017","a":"1995","b":0},
{"y":"2018","a":"1200","b":0},
{"y":"2019","a":"4990","b":"1450"},
{"y":"2019","a":"4990","b":0}]

其中yYEARSaPAIDbUNPAID

我想要实现的目标是将所有数据分组到我本可以拥有的特定年份     [{"y":"2017","a":"1995","b":0}, {"y":"2018","a":"1200","b":0}, {"y":"2019","a":"4990","b":"1450"}]

如果没有,它会复制年份,而是将它们合并为一个单元。

我需要做什么,以及需要实现哪些代码才能实现。

1 个答案:

答案 0 :(得分:0)

您只需要条件聚合吗?

// This can be merged into single import statement

import { 
    cakeChoicePricing, 
    cakeTypeResultPricing, 
    cupcakeChoicePricing, 
    cupcakeTypeResultPricing
} from './JavaScript.js';