MYSQL连接返回太多结果

时间:2018-01-29 16:39:09

标签: mysql codeigniter

所以我试图在数据库中获得一些不同的表。 这是我正在使用的查询

$this->db->select('
        c.*,
        SUM(rm.discount_value) as totalDiscount,
        SUM(a.status = 0) as totalVisits,
        SUM(a.status != 0) as totalAnnulations,
        SUM(r.treatment_price + r.arrangement_price + r.product_price) + c.start_rev as totalRevenue
    ')
        ->join('customers c','c.customer_id = a.customer_id','left')
        ->join('revenue r','r.customer_id = a.customer_id','left')
        ->join('remarks rm','rm.customer_id = a.customer_id','left')
        ->from('agenda a')
        ->where('a.user_id',$user_id)
        ->where('a.customer_id',$customer_id)
        ->group_by('a.customer_id');

这导致以下查询。

SELECT `c`.*, 
       SUM(rm.discount_value) as totalDiscount,
       SUM(a.status = 0) as totalVisits, SUM(a.status != 0) as totalAnnulations, 
       SUM(r.treatment_price + r.arrangement_price + r.product_price) + c.start_rev as totalRevenue 
FROM (`agenda` a) 
       LEFT JOIN `customers` c ON `c`.`customer_id` = `a`.`customer_id`
       LEFT JOIN `revenue` r ON `r`.`customer_id` = `a`.`customer_id`
       LEFT JOIN `remarks` rm ON `rm`.`customer_id` = `a`.`customer_id` WHERE `a`.`user_id` = '9' AND `a`.`customer_id` = '4134' 
       GROUP BY `a`.`customer_id`

此查询返回 4810 次访问,但只有 74

这会返回太多结果。就像它没有使用a.customer_id

欢迎任何帮助,提前谢谢。

1 个答案:

答案 0 :(得分:0)

您正在使用GROUP BY,而该列没有出现在SELECT中。请参阅how to use GROUP BY