缺席日期的Mysql逻辑

时间:2013-06-03 08:02:50

标签: mysql sql

我有3个表如何使用manageriD

获取特定日期范围内所有员工的缺席

1)查找表中的日期(可查找所有月份)* /     例子

╔════════════╗
║    DATE    ║
╠════════════╣
║ 2013-01-01 ║
║ 2013-01-02 ║
║ 2013-01-03 ║
╚════════════╝

第二个员工表格,其中包含诸如(所有具有经理ID的员工的员工表格)的列

╔════════════╦══════════╦════════╦══════════╦═══════╦═══════════╗
║ EMPLOYEEID ║ BRANCHID ║ DEPTID ║ DESIGNID ║ NAME  ║ MANAGERID ║
╠════════════╬══════════╬════════╬══════════╬═══════╬═══════════╣
║          1 ║        1 ║     10 ║        5 ║ A     ║        15 ║
║          2 ║        2 ║     10 ║        5 ║ AB    ║        18 ║
║          3 ║        1 ║     10 ║        5 ║ ABC   ║        15 ║
║          5 ║        1 ║     10 ║        5 ║ ABCD  ║        15 ║
║          9 ║        2 ║     10 ║        5 ║ AEFG  ║        16 ║
║         12 ║        1 ║     10 ║        5 ║ AHIJ  ║        15 ║
║          8 ║        1 ║     10 ║        5 ║ AKLMN ║        18 ║
╚════════════╩══════════╩════════╩══════════╩═══════╩═══════════╝ 

第三个表是登录和表格(所有员工的登录和退出)

╔════════════╦══════════╦═════════════════════╦═════════════╗
║ EMPLOYEEID ║ BRANCHID ║    CHECKLOGSDATE    ║ CHECKINTYPE ║
╠════════════╬══════════╬═════════════════════╬═════════════╣
║          1 ║        1 ║ 2013-01-01 09:12:15 ║ in          ║
║          1 ║        2 ║ 2013-01-01 19:24:15 ║ out         ║
║          2 ║        1 ║ 2013-01-01 10:20:15 ║ in          ║
║          2 ║        1 ║ 2013-01-01 18:24:15 ║ out         ║
║          3 ║        2 ║ 2013-01-01 09:12:15 ║ in          ║
║          3 ║        1 ║ 2013-01-01 16:24:15 ║ out         ║
║          4 ║        1 ║ 2013-01-01 09:32:15 ║ in          ║
╚════════════╩══════════╩═════════════════════╩═════════════╝

SQLFiddle Demo

2 个答案:

答案 0 :(得分:1)

目前还不清楚你的问题是什么规则告诉员工在任何一天都缺席。如果日志中没有任何记录(输入或输出),那么您可以尝试

<强>更新

SELECT d.date, e.employeeid, e.name
  FROM lookup d CROSS JOIN employee e LEFT JOIN log l 
    ON d.date = DATE(l.Checklogsdate)
   AND e.employeeid = l.employeeid   
 WHERE e.managerid = 15  
   AND d.date BETWEEN '2013-01-01' AND '2013-01-02'
   AND l.employeeid IS NULL
 GROUP BY d.date, e.employeeid, e.name 

这是 SQLFiddle 演示。

<强>原始

SELECT e1.date, e1.employeeid, e1.name
  FROM
(
 SELECT d.date, e.employeeid, e.name
   FROM lookup d CROSS JOIN employee e
   WHERE managerid = 15  
     AND d.date BETWEEN '2013-01-01' AND '2013-01-02'
) e1 LEFT JOIN
(
 SELECT l.employeeid, DATE(MIN(l.Checklogsdate)) date
   FROM log l JOIN employee e 
     ON l.employeeid = e.employeeid
    AND e.managerid = 15  
  GROUP BY l.employeeid, DATE(l.Checklogsdate)
) l1
    ON e1.date = l1.date
   AND e1.employeeid = l1.employeeid 
 WHERE l1.employeeid IS NULL
 ORDER BY e1.date, e1.employeeid

示例输出:

|             DATE | EMPLOYEEID | NAME |
----------------------------------------
| January, 01 2013 |          5 | ABCD |
| January, 01 2013 |         12 | AHIJ |
| January, 02 2013 |          1 |    A |
| January, 02 2013 |          3 |  ABC |
| January, 02 2013 |          5 | ABCD |
| January, 02 2013 |         12 | AHIJ |

这是 SQLFiddle 演示。

答案 1 :(得分:0)

SELECT e.Name
FROM Employee e
WHERE e.ManagerId = 15
AND e.Name NOT IN
(
    SELECT DISTINCT e.Name
    FROM Log l
    INNER JOIN Employee e ON l.EmployeeId = e.EmployeeId
    WHERE e.ManagerId = 15
)

此处15将替换为您想要的号码

SQL Fiddle DEMO