我们如何从Cakephp中的两个表中提取两列的SUM

时间:2015-06-15 09:22:42

标签: mysql cakephp sum database-table

我有两个名为SalaryPayment和SalaryStaff的表。

SalaryStaff表

+-----------------+------------+-------------+
| SalaryStaffId   | EmployeeId | TotalWage   |    
+-----------------+------------+-------------+
|      6          |    5       |    80       |  
|      7          |    5       |    100      |
|      9          |    5       |    60       | 
+-----------------+------------+-------------+

SalaryPayment表

+-----------------+------------+-------------+
| SalaryPaymentId | EmployeeId | PaidAmount  |    
+-----------------+------------+-------------+
|      1          |    5       |    550      |  
+-----------------+------------+-------------+

我需要一个如下结果,

+-----------------+-----------------+-----------------+-----------------+
|  EmployeeId     | TotalPaidAmount | TotalWageAmount | PendingAmount   |
+-----------------+-----------------+-----------------+-----------------+
|      5          |    550          |    240          |     210         |
+-----------------+-----------------+-----------------+-----------------+
  

TotalPaidAmount表示SUM(SalaryPayment.PaidAmount)

     

TotalWageAmount表示SUM(SalaryStaff.TotalWage)

     

PendingAmount表示SUM(SalaryStaff.TotalWage) - SUM(SalaryPayment .PaidAmount)

我尝试过以下操作,它使TotalPaidAmount获得三倍的实际值为1650,TotalWageAmount获得正确的值。

$this->SalaryStaff->find('all',array(
                'fields'=>array('SalaryPayment.*','SalaryStaff.*','SUM(SalaryStaff.TotalWage) as TotalWageAmount','SUM(SalaryPayment.PaidAmount) as TotalPaidAmount'),
                'joins'=>array(
                    array(                      
                        'alias'=>'SalaryPayment',
                        'table'=>'salary_payments',                     
                        'conditions' =>array('SalaryPayment.EmployeeId = SalaryStaff.EmployeeId')
                        )
                    ),
                'conditions' =>array('SalaryStaff.EmployeeId'=>$EmployeeId),
                'group' => 'SalaryStaff.EmployeeId'
                ));

2 个答案:

答案 0 :(得分:1)

对于您的具体问题,最好只运行两个find来检索该员工所需的两个值。

要实现单个查询解决方案,最简单的方法是先了解如何编写SQL,然后找出Cakephp。每个表的行首先需要“Grouped”,然后“Join”-ed。

SELECT SalaryStaffGrouped.EmployeeId
     , COALESCE(TotalPaidAmount, 0) AS TotalPaidAmount
     , TotalWageAmount
     , COALESCE(TotalPaidAmount,0)-TotalWageAmount AS PendingAmount
FROM (SELECT EmployeeId, SUM(TotalWage) AS TotalWageAmount
      FROM SalaryStaff
      GROUP BY EmployeeId) AS SalaryStaffGrouped
LEFT JOIN (SELECT EmployeeId, SUM(PaidAmount) AS TotalPaidAmount
      FROM SalaryPayment) AS SalaryPaymentGrouped
  ON SalaryStaffGrouped.EmployeeID = SalaryPaymentGrouped.EmployeeId;

有几种方法可以在Cakephp中处理实现。我建议query method轻松一点。您还可以根据这些分组/汇总表创建一些MySQL视图,然后根据这些视图构建Cakephp模型。

您需要注意,这只会列出SalaryStaff表中找到的员工。如果员工只存在于SalaryPayment表中(而不是SalaryStaff表),则不会列出该人员。

如果需要按EmployeeId进行过滤,则通过向每个子查询添加两个WHERE子句而不是整个查询,查询将更好地执行。

答案 1 :(得分:1)

我认为这个查询会有所帮助:)

SELECT
ss.EmployeeId,
sp.PaidAmount,
SUM(ss.TotalWage) as TotalWageAmount,
sp.PaidAmount - SUM(ss.TotalWage) as PendingAmount
FROM
SalaryStaff ss
left join SalaryPayment sp on (ss.employeeId = sp.employeeid)
group by
ss.employeeid,
sp.PaidAmount