我遇到了复杂的MySQL查询。我有以下表格:
[employee]
id
name
[department]
id
name
[employee_department_relation]
id
employee_id
department_id
[attendance]
id
employee_id
date
注意:仅当员工在该日期在场时才会记录出勤率。没有为abseentism设置特定标志。
我需要知道,特定日期缺少哪些部门的员工。
到目前为止,我已经完成了SQL,但是,它并不完美:
SELECT A.date,
(SELECT name FROM employee WHERE id = emp_id) AS employee,
(SELECT D.name AS dept_name
FROM deptartment D
INNER JOIN employee_department_relation R ON R.dept_id = D.id
INNER JOIN employee E ON E.id = R.emp_id
WHERE R.emp_id = A.emp_id
) AS dept
FROM attendance A
WHERE A.date = '2014-07-03'
我该怎么办?
答案 0 :(得分:3)
根据需要尝试更改日期:
select employee.name, department.name from employee
left join employee_department_relation r on r.employee_id = employee.id
left join department on department.id = r.department_id
where employee.id not in (
select employee_id from attendance where date = '2014-07-28'
)