所以我试图在数据库中获得一些不同的表。 这是我正在使用的查询
$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
欢迎任何帮助,提前谢谢。