两个日期

时间:2015-10-14 19:41:32

标签: php mysql date

如果当前日期介于emphiredate(雇用雇用日期)加上一年之间,我如何显示结果 查询效果很好。

   SELECT 
        e.empFirst AS employees, e.holidays,
            @year := CEIL(DATEDIFF(h.date, e.empHireDate - interval 1 day)/365) AS Year,
               CONCAT(DATE_ADD(e.empHireDate, INTERVAL @year-1 YEAR),' to ',DATE_ADD(e.empHireDate  - interval 1 day, INTERVAL @year YEAR)) AS jon,
        COUNT(h.date) AS Days,
        SUM(h.hours) AS Hours,
       e.holidays - SUM(h.hours) AS Hours_Left
    FROM employees AS e
    LEFT JOIN holidaydays AS h ON e.empid = h.booking_id
    WHERE e.empid = booking_id and hours !=0
    GROUP BY Year, booking_id

结果:

employees  holidays Year    Date                   Days    Hours  Hours_Left
Jonathan    200      1   2014-10-10 to 2015-10-09   1       8          192
Arthur       0       1   2015-08-02 to 2016-08-01   1       10         -10
Mark        200      1   2014-10-10 to 2015-10-09   1       9          191
Jonathan    200      2   2015-10-10 to 2016-10-09   24      162         38
Jonathan    200      3   2016-10-10 to 2017-10-09    2      19         181
Mark        200      3   2016-10-10 to 2017-10-09    1       8         192

因为今天的日期是2015-10-14,我想要的结果是

employees  holidays Year    Date                   Days    Hours  Hours_Left
Arthur       0       1   2015-08-02 to 2016-08-01   1       10         -10
Jonathan    200      2   2015-10-10 to 2016-10-09   24      162         38

这是因为今天的日期介于日期之间,任何人都可以帮忙。

SQL FIDDLE DEMO

1 个答案:

答案 0 :(得分:0)

and curdate() > DATE_ADD(e.empHireDate, INTERVAL CEIL(DATEDIFF(h.date, e.empHireDate - interval 1 day)/365)-1 YEAR)
and curdate() < DATE_ADD(e.empHireDate  - interval 1 day, INTERVAL CEIL(DATEDIFF(h.date, e.empHireDate - interval 1 day)/365) YEAR)

编辑:现在应该使用您的动态日期 - 另外值得一提的是您的SQL Fiddle模式与您的示例有不同的雇用。