MYSQL |今天和最后日期从表格中获取所有员工的日期

时间:2015-01-02 07:33:54

标签: php mysql

我正在尝试创建一个查询,以获取今天和最后一次约会的所有员工。

我有两列in_date和out_date

所以,如果列in_dateout_date都有记录,我不想获取这些记录。

我想只获取今天和昨天日期的记录,其中设置了in_date但没有设置out_date加上那些甚至没有in_date和out_date记录的记录......

因此,简单来说,它将从出勤表中获取其中未设置in_date和out_date的所有记录。如果两者都设置,那么不要获取记录..

这是我到目前为止的查询,不确定我是否采取了正确的方式..

SELECT 
  `E`.`employee_id` AS EmployeeID,
  `E`.`full_name` AS EmployeeNam,
  `ATT`.`in_date` AS EmployeeInDate,
  `ATT`.`out_date` AS EmployeeOutDate,
  `ATT`.`in_time` AS EmployeeInTime,
  `ATT`.`out_time` AS EmployeeOutTime 
FROM
  (`employee` E) 
  INNER JOIN `employment` ET 
    ON `E`.`employee_id` = `ET`.`employee_id` 
    AND ET.current = 1 
  INNER JOIN `employee_project` EP 
    ON `E`.`employee_id` = `EP`.`employee_id` 
  LEFT JOIN `attendence` ATT 
    ON `ATT`.`employee_id` = `E`.`employee_id`  
  INNER JOIN `posting` P 
    ON `P`.`employement_id` = `ET`.`employment_id` 
    AND P.current = 1 
    AND P.status = 2 
WHERE `E`.`enrolled` = 1 -- AND ATT.`in_date` = CURDATE() - 1 OR ATT.`in_date` = CURDATE()
GROUP BY `E`.`employee_id`

这是我现在得到的记录。 enter image description here

我在

中尝试了这个
AND ATT.`in_date` = CURDATE() - 1 OR ATT.`in_date` = CURDATE()

不确定它是获得今天和昨天日期的正确方法吗?但如果不对,请告诉我们今天和以前的日期是什么样的正确方法。

加上它不会让我记录空日期,我怎么会得到空日期?

注意空: 我想要空的只是如果员工今天没有参加,所以它是空的,那么我想今天空或最后一天如果存在空(不是所有的空记录)

- = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - =

更新 这次我尝试了UNION方式..但是对于NULL,union也会同时使用这些空值吗?那么如何区分null是哪个日期??

SELECT 
  `E`.`employee_id` AS EmployeeID,
  `E`.`full_name` AS EmployeeNam,
  `ATT`.`in_date` AS EmployeeInDate,
  `ATT`.`out_date` AS EmployeeOutDate,
  `ATT`.`in_time` AS EmployeeInTime,
  `ATT`.`out_time` AS EmployeeOutTime 
FROM
  (`employee` E) 
  INNER JOIN `employment` ET 
    ON `E`.`employee_id` = `ET`.`employee_id` 
    AND ET.current = 1 
  INNER JOIN `employee_project` EP 
    ON `E`.`employee_id` = `EP`.`employee_id` 
  LEFT JOIN `attendence` ATT 
    ON `ATT`.`employee_id` = `E`.`employee_id`  AND ATT.`in_date` = CURDATE() - 1 -- Get Yesterdays Records
  INNER JOIN `posting` P 
    ON `P`.`employement_id` = `ET`.`employment_id` 
    AND P.current = 1 
    AND P.status = 2 
WHERE `E`.`enrolled` = 1
GROUP BY `E`.`employee_id` , ATT.in_date
UNION
SELECT 
  `E`.`employee_id` AS EmployeeID,
  `E`.`full_name` AS EmployeeNam,
  `ATT`.`in_date` AS EmployeeInDate,
  `ATT`.`out_date` AS EmployeeOutDate,
  `ATT`.`in_time` AS EmployeeInTime,
  `ATT`.`out_time` AS EmployeeOutTime 
FROM
  (`employee` E) 
  INNER JOIN `employment` ET 
    ON `E`.`employee_id` = `ET`.`employee_id` 
    AND ET.current = 1 
  INNER JOIN `employee_project` EP 
    ON `E`.`employee_id` = `EP`.`employee_id` 
  LEFT JOIN `attendence` ATT 
    ON `ATT`.`employee_id` = `E`.`employee_id` AND ATT.`in_date` = CURDATE() -- Get Todays Dates
  INNER JOIN `posting` P 
    ON `P`.`employement_id` = `ET`.`employment_id` 
    AND P.current = 1 
    AND P.status = 2 
WHERE `E`.`enrolled` = 1
GROUP BY `E`.`employee_id`

enter image description here

3 个答案:

答案 0 :(得分:0)

你可以在mysql中尝试类似的东西:

WHERE (ATT.in_date BETWEEN DATE_SUB(CURRENT_TIMESTAMP, INTERVAL 1 DAY) AND CURRENT_TIMESTAMP)

DATE_SUB(CURRENT_TIMESTAMP,INTERVAL 1天) - >今天约会 - 1天。

看一下这个函数DATE_SUB

我希望它有所帮助。

答案 1 :(得分:0)

如果我理解正确的话:

AND ATT.in_date BETWEEN CONCAT(CAST(DATE(CURDATE() - 1) AS CHAR), ' 00:00:00') AND CONCAT(CAST(DATE(CURDATE()) AS CHAR), ' 23:59:59') AND (ATT.out_date IS NULL OR ATT.out_date = '0000-00-00 00:00:00')

应该给你你想要的东西,但它有点乱:p

<强>更新 如果您还想要in_date和out_date为NULL的记录,请尝试这样做:

AND (ATT.in_date BETWEEN CONCAT(CAST(DATE(CURDATE() - 1) AS CHAR), ' 00:00:00') AND CONCAT(CAST(DATE(CURDATE()) AS CHAR), ' 23:59:59') AND (ATT.out_date IS NULL OR ATT.out_date = '0000-00-00 00:00:00')) OR (ATT.in_date IS NULL OR ATT.in_date = '0000-00-00 00:00:00' AND ATT.out_date IS NULL OR ATT.out_date = '0000-00-00 00:00:00')

答案 2 :(得分:0)

AND (
      (ATT.`in_date` >= DATE_SUB(CURDATE(), INTERVAL 1 DAY) AND (ATT.`out_date` IS NULL OR ATT.`out_date` = '0000-00-00'))
    OR 
      ((ATT.`in_date` IS NULL OR ATT.`in_date` = '0000-00-00') AND (ATT.`out_date` IS NULL OR ATT.`out_date` = '0000-00-00'))
    )

以上条件应返回以下记录:

  • ATT.in_date或者等于或大于昨天的日期(假设您将来没有记录) AND ATT.out_date未设置(NULL或&#39; 0000-00-00&#39)
  • ATT.in_date未设置(NULL或&#39; 0000-00-00&#39;) AND ATT.out_date未设置(NULL或&#39; 0000-00-00&#39;。)