php—sum中的子查询或sum有可能吗?

fquxozlt  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(454)

我需要一个问题的帮助,有代码和总和,在这个总和之后,我需要实现另一个和得到的结果。
分组和求和后是否可以运行查询?我的代码:

$start = "2018-08-01";
$end = "2018-08-30";

$this->db->where("DATE(launched_on) BETWEEN DATE('".$start."') AND DATE('".$end."')");

$this->db->select("*, 
    payment_method,sum(value) as Total, 
    SUM(value * (REPLACE(comission, '1/', '') / 100)) as commission_new, 
    GROUP_CONCAT(id separator ';')  as all_ids, 
    SUM(IF(received_employee = 'yes',
    value * (REPLACE(comission, '1/', '') / 100) , 0)) as pay_value, 
    sum(amount) as Total_sale");

$this->db->group_by('order_id, product');

$this->db->where("type_transaction='products'");

$teste = $this->db->get_where('cashier_transaction', 
array('company_id'=>"1", 
    'type_payment'=>'finish_order', 
    'type'=>'revenue', 
    'product'=>'1'))->result();

结果代码:

[0] => stdClass Object
    (
        [amount] => 1
        [order_id] => 11307
        [product] => 1
        [payment_method] => 2
        [Total] => 76
        [commission_new] => 13.68
        [all_ids] => 15242
        [pay_value] => 13.68
        [Total_sale] => 1
    )

[1] => stdClass Object
    (
        [amount] => 1
        [order_id] => 11402
        [product] => 1
        [payment_method] => 3
        [Total] => 76
        [commission_new] => 13.68
        [all_ids] => 15350
        [pay_value] => 13.68
        [Total_sale] => 1
    )

[2] => stdClass Object
    (
        [amount] => 1
        [order_id] => 11536
        [product] => 1
        [payment_method] => 3
        [Total] => 76
        [commission_new] => 13.68
        [all_ids] => 15532
        [pay_value] => 13.68
        [Total_sale] => 1
    )

[3] => stdClass Object
    (
        [amount] => 1
        [order_id] => 11546
        [product] => 1
        [payment_method] => 3
        [Total] => 76
        [commission_new] => 13.68
        [all_ids] => 15549
        [pay_value] => 13.68
        [Total_sale] => 1
    )

[4] => stdClass Object
    (
        [amount] => 1
        [order_id] => 11616
        [product] => 1
        [payment_method] => 2
        [Total] => 76
        [commission_new] => 13.68
        [all_ids] => 15637
        [pay_value] => 13.68
        [Total_sale] => 1
    )

obs:我需要团体订单号,因为有多种付款方式。
我需要这个结果:(这将是所有值的总和“总计,佣金\新,支付\价值和一组产品”)

[0] => stdClass Object
    (
        [product] => 1
        [Total] => 380
        [commission_new] => 68.35
        [all_ids] => 15837;15549;15532;15242
        [pay_value] => 68.35
        [Total_sale] => 5
    )

有可能吗?

vi4fp9gy

vi4fp9gy1#

解决方案:

$start = "2018-07-01";
    $end = "2018-08-02";
    $teste = $this->db->query("select product, 
        SUM(Total) as total, SUM(commission_new) as commission, 
        GROUP_CONCAT(all_ids separator ';')  as all_idss, 
        SUM(Total_sale) as total_sale  from (SELECT amount, 
        SUM(if(installment_total = installment, amount, 0)) as Total_sale, 
        order_id,product, 
        payment_method,
        sum(value) as Total, 
        SUM(value * (REPLACE(comission, '1/', '') / 100)) as commission_new, 
        GROUP_CONCAT(order_id separator ';')  as all_ids, 
        SUM(IF(received_employee = 'yes', value * (REPLACE(comission, '1/', '') / 100) , 0)) as pay_value FROM cashier_transaction 
        WHERE company_id='1' and 
        type_payment='finish_order' and 
        type='revenue' and 
        type_transaction='products' and 
        product='1' and 
        DATE(launched_on) BETWEEN DATE(?) AND DATE(?) 
        GROUP BY order_id, product) 
        p group by product", array($start,$end))->result();
    print_r($teste);

结果:

[0] => stdClass Object
    (
        [product] => 1
        [total] => 4332
        [commission] => 779.7599999999992
        [all_idss] => 4654;4654;4689;4742;4742;4888;4894;5020;5492;5593;5843;6386;6539;6849;6849;6976;6980;6983;7159;7183;7308;7690;7775;7903;7903;7942;7987;8054;8222;8359;8445;8627;8684;8807;9028;9256;9300;9423;9684;9858;9998;9999;9999;10023;10024;10026;10169;10361;10365;10366;10442;10557;10587;10642;10718;11307;11402;11536;11546;11616
        [total_sale] => 57
    )

相关问题