mysql中的netsalary代码加倍

mysql中的netsalary代码加倍,mysql,join,Mysql,Join,请任何人告诉我为什么下面的代码不从扣减表中加总金额,然后从考勤表中的金额中扣除以获得净工资。我非常感谢您,在申请扣除之前,我不会将考勤表中的金额加起来再扣除一倍 select sum(attendance.amount) - max(deduction.amount) from attendance join deduction on attendance.staffid = deduction.staffid where attendance.staffid = some_staffid

请任何人告诉我为什么下面的代码不从扣减表中加总金额,然后从考勤表中的金额中扣除以获得净工资。我非常感谢您,在申请扣除之前,我不会将考勤表中的金额加起来再扣除一倍

select sum(attendance.amount) - max(deduction.amount) 
from attendance 
join deduction on attendance.staffid = deduction.staffid 
where attendance.staffid = some_staffid 
    and month(attendance.date) = some_month
    and month(deduction.date_approved) = some_month

只是一个粗略的猜测-每个考勤都有2条扣减记录=>联接将复制考勤表中的金额=>总和将对这些重复的记录求和

简单的解决方案(但不是很好地执行,我感到震惊)是:

select sum(a) - max_d
from (
  select attendance.amount a, deduction.amount d, max(deduction.amount) max_d
  from attendance 
  join deduction on attendance.staffid = deduction.staffid 
  where attendance.staffid = some_staffid 
      and month(attendance.date) = some_month
      and month(deduction.date_approved) = some_month
)
where d = max_d

非常感谢,请您解释一下“表现不太好”。好吧,如果您有数百万张唱片,可能需要一段时间才能完成;)