简体   繁体   English

CodeIgniter活动记录计数mysql记录根据值

[英]CodeIgniter active record count mysql records according value

What would be wrong with my active record query the SUM function works fine but the count parts returns the count of all records. 我的活动记录查询有什么问题,SUM函数可以正常工作,但计数部分返回所有记录的计数。

$this->db->select(
                        '
                         t1.account_balance, 
                         t2.CustomerName AS customer,
                         t2.CustomerId,
                         SUM(IF(t3.Bargain="Sale",t3.TotalAmount,0)) AS total_sale,
                         SUM(IF(t3.Bargain="Purchase",t3.TotalAmount,0)) AS total_buy,
                         COUNT(IF(t3.Bargain="Sale",t3.Bargain,0)) AS count_sale,
                         COUNT(IF(t3.Bargain="Purchase",t3.Bargain,0)) AS count_buy,
                         '
                     ,FALSE);
    $this->db->from("balances AS t1");
    $this->db->join("customer AS t2","t2.CustomerId = t1.customer_id","left");
    $this->db->join("gold_order AS t3","t3.CustomerId = t2.CustomerId","left");
    $this->db->group_by("t3.CustomerId");
    $object = $this->db->get();

Result: 结果:

在此处输入图片说明

What I want the count_sale should be 3 and count_buy should be 4: 我想要count_sale应该是3而count_buy应该是4:

在此处输入图片说明

try this 尝试这个

$this->db->select(
                    '
                     t1.account_balance, 
                     t2.CustomerName AS customer,
                     t2.CustomerId,
                     SUM(IF(t3.Bargain="Sale",t3.TotalAmount,0)) AS total_sale,
                     SUM(IF(t3.Bargain="Purchase",t3.TotalAmount,0)) AS total_buy,
                     SUM(IF(t3.Bargain="Sale",1,0)) AS count_sale,
                     SUM(IF(t3.Bargain="Purchase",1,0)) AS count_buy,
                     '
                 ,FALSE);
$this->db->from("balances AS t1");
$this->db->join("customer AS t2","t2.CustomerId = t1.customer_id","left");
$this->db->join("gold_order AS t3","t3.CustomerId = t2.CustomerId","left");
$this->db->group_by("t3.CustomerId");
$object = $this->db->get();

Try this: 尝试这个:

$this->db->select('
                         t1.account_balance, 
                         t2.CustomerName AS customer,
                         t2.CustomerId,
                         SUM(IF(t3.Bargain="Sale",t3.TotalAmount,0)) AS total_sale,
                         SUM(IF(t3.Bargain="Purchase",t3.TotalAmount,0)) AS total_buy,
                        (SELECT count(*) from gold_order where gold_order.Bargain="Sale") AS count_sale,
                         (SELECT count(*) from gold_order where gold_order.Bargain="Purchase") AS count_buy,
                         '
                     ,FALSE);
    $this->db->from("balances AS t1");
    $this->db->join("customer AS t2","t2.CustomerId = t1.customer_id","left");
    $this->db->join("gold_order AS t3","t3.CustomerId = t2.CustomerId","left");
    $this->db->group_by("t3.CustomerId");
    $object = $this->db->get();

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

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