这是4张桌子......
我想通过此查询找出员工缺席报告....但是我需要为很多员工应用此报告...有什么方法可以简化此查询吗?
SELECT date
FROM tbl_std_working_hour
WHERE date NOT IN (SELECT date FROM tbl_attendance WHERE emp_id = '$emp_id')
AND date NOT IN (SELECT date FROM tbl_holiday)
AND date NOT IN (SELECT date FROM tbl_leave WHERE emp_id = '$emp_id')
AND total_hour <> '00:00:00'
AND date >= '$start'
AND date <= '$end'
AND emp_id = '$emp_id'
答案 0 :(得分:2)
首先,我会使用NOT EXISTS
重写:
SELECT wh.date
FROM tbl_std_working_hour wh
WHERE NOT EXISTS (SELECT 1
FROM tbl_attendance a
WHERE a.date = wh.date AND a.emp_id = wh.emp_id
) AND
NOT EXISTS (SELECT 1
FROM tbl_holiday h
WHERE h.date = wh.date
) AND
NOT EXISTS (SELECT 1
FROM tbl_leave l
WHERE l.emp_id = wh.emp_id and l.date = wh.date
)
WHERE wh.total_hour <> '00:00:00' AND
wh.date >= '$start' AND
wh.date <= '$end' AND
wh.emp_id = '$emp_id';
然后添加以下复合(多列)索引:
tbl_std_working_hour(emp_id, date, total_hour)
tbl_attendance(emp_id, date)
tbl_holiday(date)
(如果date
是主键或唯一身份,则可能已存在)tbl_leave(emp_id)
(如果emp_id
是主键或唯一身份,则可能已存在)请注意,我更改了子查询以引用外部查询中的emp_id
。这样可以更轻松地更改emp_id
。此外,您的查询应该使用WHERE
子句中的值的参数。
答案 1 :(得分:0)
这也是使用UNION ALL
SELECT date
FROM tbl_std_working_hour AS tswh
WHERE NOT EXISTS(
SELECT date FROM tbl_attendance ta WHERE ta.date = tswh.date AND ta.emp_id = tswh.emp_id
UNION ALL
SELECT date FROM tbl_holiday th WHERE th.date = tswh.date
UNION ALL
SELECT date FROM tbl_leave tl WHERE tl.date = tswh.date AND tl.emp_id = tswh.emp_id)
AND total_hour <> '00:00:00'
AND date >= '$start'
AND date <= '$end'
AND emp_id = '$emp_id'