多个左连接

时间:2015-10-07 04:34:24

标签: mysql sql left-join

这是我的sql select语句http://www.sqlfiddle.com/#!9/353e82/1
这是输出Output
我使用左连接,但数据将在输出上复制 我想要的是数据不会复制,只显示一次 的收益

  • 总OT支付金额
  • 运输 - 金额
  • 统一 - 金额

推导

  • 贷款 - 金额
  • 未付假期 - 金额

由于

1 个答案:

答案 0 :(得分:0)

如果您想要基于payslip_no的单行,请使用以下查询 -

SELECT
     hrms_salary_payment.`payslip_no` AS payslip_no,
     hrms_salary_payment.`payment_month` AS payment_month,
     hrms_salary_payment.`basic_salary` AS basic_salary,
     hrms_salary_payment.`hours_worken_week` AS hours_worken_week,
     hrms_salary_payment.`ot_hours_worked` AS ot_hours_worked,
     hrms_salary_payment.`total_ot_pay` AS total_ot_pay,
     hrms_salary_payment.`employee_cpf` AS employee_cpf,
     hrms_salary_payment.`employer_cpf` AS employer_cpf,
     hrms_salary_payment.`sdl_payable` AS sdl_payable,
     hrms_salary_payment.`net_salary` AS net_salary,
     hrms_salary_payment.`payment_amount` AS payment_amount,
     hrms_salary_payment.`payment_date` AS payment_date,
     hrms_salary_additional.`item_remark` AS income,
     hrms_salary_additional.`item_amount` AS income_amount,
     hrms_salary_additional.`isCPF` AS isCPF,
     hrms_salary_deduction.`deduction_remark` AS deduction_remark,
     hrms_salary_deduction.`deduction_amount` AS deduction_amount
     FROM `hrms_salary_payment` 
     LEFT JOIN `hrms_salary_additional` ON hrms_salary_additional.payment_id = hrms_salary_payment.payment_id
     LEFT JOIN `hrms_salary_deduction` ON hrms_salary_deduction.payment_id = hrms_salary_payment.payment_id
     where hrms_salary_payment.`payment_id` = 27 
     GROUP BY hrms_salary_payment.`payslip_no`;

但是如果你想通过item_remarks进一步区分3行,那么使用下面的查询 -

SELECT
     hrms_salary_payment.`payslip_no` AS payslip_no,
     hrms_salary_payment.`payment_month` AS payment_month,
     hrms_salary_payment.`basic_salary` AS basic_salary,
     hrms_salary_payment.`hours_worken_week` AS hours_worken_week,
     hrms_salary_payment.`ot_hours_worked` AS ot_hours_worked,
     hrms_salary_payment.`total_ot_pay` AS total_ot_pay,
     hrms_salary_payment.`employee_cpf` AS employee_cpf,
     hrms_salary_payment.`employer_cpf` AS employer_cpf,
     hrms_salary_payment.`sdl_payable` AS sdl_payable,
     hrms_salary_payment.`net_salary` AS net_salary,
     hrms_salary_payment.`payment_amount` AS payment_amount,
     hrms_salary_payment.`payment_date` AS payment_date,
     hrms_salary_additional.`item_remark` AS income,
     hrms_salary_additional.`item_amount` AS income_amount,
     hrms_salary_additional.`isCPF` AS isCPF,
     hrms_salary_deduction.`deduction_remark` AS deduction_remark,
     hrms_salary_deduction.`deduction_amount` AS deduction_amount
     FROM `hrms_salary_payment` 
     LEFT JOIN `hrms_salary_additional` ON hrms_salary_additional.payment_id = hrms_salary_payment.payment_id
     LEFT JOIN `hrms_salary_deduction` ON hrms_salary_deduction.payment_id = hrms_salary_payment.payment_id
     where hrms_salary_payment.`payment_id` = 27 
     GROUP BY hrms_salary_payment.`payslip_no`, hrms_salary_additional.`item_remark`;