好的,所以我通过使用如下视图来解决我的问题:
1-查看total_points_plus(所有加号为'+'的点)
create view total_points_plus as select customer_id , sum(number) as plus from points where (customer_id in (select id from ecom_registered_users) and plus_minus = '+') group by customer_id
2-查看total_points_plus(所有加号为'-'的点)
create view total_points_minus as select customer_id , sum(number) as minus from points where (customer_id in (select id from ecom_registered_users) and plus_minus = '-') group by customer_id
3-视图对以上视图进行简单计算(总计)
create VIEW total_points AS select total_points_plus.customer_id , plus , IFNULL( minus , 0) as minus , (plus - IFNULL(minus , 0)) as grand_total from total_points_plus left join total_points_minus on total_points_plus.customer_id = total_points_minus.customer_id
4- 将最终报告也作为视图获取
create view final_report as select ecom_registered_users.userName as name , ecom_registered_users.userEmail as email , ecom_registered_users.userMobile as mobile , total_points.grand_total as total from ecom_registered_users left join total_points on ecom_registered_users.id = total_points.customer_id
就是这样,然后在我的 php 导出代码中,我简单而优雅地执行此查询:
<?php $query = "SELECT * FROM final_report " ; ?>