要联接具有多个条件的多个表

时间:2018-12-17 18:40:15

标签: mysql jdbc inner-join

下面是我的查询

SELECT salary.id,
       employee.emp_id,
       employee.emp_name,
       department.dept_name,
       designation.desig_name,
       salary.basic,
       salary.house_rent + salary.conveyance + salary.medical + salary.dearness + salary.others_allowances AS allowances,
       salary.income_tax + salary.pro_tax + salary.emp_state_insu + salary.others_deductions AS deductions,
       Sum(attendance.attendance = 'Absent') * salary.absence_fine AS fine,
       Monthname(attendance.date) AS month,
       salary.month AS monYear
  FROM employee
       INNER JOIN attendance ON employee.emp_id = attendance.emp_id
       INNER JOIN department ON employee.dept_id = department.dept_id
       INNER JOIN designation ON employee.desig_id = designation.desig_id
       INNER JOIN salary ON designation.desig_id = salary.desig_id
  GROUP BY salary.id,
           employee.emp_id,
           Monthname(attendance.date)  

我的输出是:

image link

请帮助我解决这个问题

1 个答案:

答案 0 :(得分:0)

您需要一个将出勤与薪水相关的列,假设您在每个表上都有一个名为month_id的列,然后您可以在最后一个内部联接中添加条件。 像这样的东西:

INNER JOIN salary ON designation.desig_id = salary.desig_id and attendace.monthId = salary.month_id