mysql |计算所选月份的日期范围内的天数

时间:2015-03-06 08:39:15

标签: mysql database

我有一个查询。查询都很好。只剩下一个合乎逻辑的事情,即从所选月份的日期范围中取出总天数。

e-g i have to columns 
1 ) project_assign_date     //considering as start date
2 ) project_revoke_date     // considering as end date

这是我的总查询。

SELECT 
  E.employee_id AS EmployeeID,
  ET.employment_id AS EmploymentID,
  E.employee_code AS EmployeeCode,
  EP.project_id AS ProjectID,
  MLP.project_title AS ProjectTitle,
  S.base_salary AS EmployeeBaseSalary,
  IFNULL(EP.percentCharged, 0) AS PercentCharged,
  A.allowance_amount AS EmployeeAllowance,
  SUM(IFNULL(A.allowance_amount,0)) + IFNULL(S.base_salary, 0) AS MonthlySalary ,
  project_assign_date AS ProjectAssignDate

FROM
  (`employee_project` EP) 
  INNER JOIN `employment` ET 
    ON `ET`.`employment_id` = `EP`.`employment_id` 
    AND ET.trashed = 0 
    AND ET.current = 1 
  INNER JOIN `employee` E 
    ON `E`.`employee_id` = `ET`.`employee_id` 
    AND E.trashed = 0 
    AND E.enrolled = 1 
  INNER JOIN `ml_projects` MLP 
    ON `MLP`.`project_id` = `EP`.`project_id` 
    AND MLP.trashed = 0 
  INNER JOIN `salary` S 
    ON `S`.`employement_id` = `ET`.`employment_id` 
    AND S.trashed = 0 
    AND S.status = 2 
  LEFT JOIN `allowance` A 
    ON `A`.`employment_id` = `ET`.`employment_id` 
    AND A.trashed = 0 
    AND A.status = 2 
  LEFT JOIN `expense_claims` EC ON EC.employment_id = ET.employment_id AND EC.status = 2
WHERE `EP`.`trashed` = 0 
  AND EP.current = 1
  AND CURRENT_DATE() >= MONTH(project_assign_date)
  GROUP BY
  E.employee_id,ET.employment_id,EP.project_id

我的结果很好,如下图所示。

Array
(
    [0] => stdClass Object
        (
            [EmployeeID] => 1
            [EmploymentID] => 15
            [EmployeeCode] => E-01
            [EmployeeName] => Yasir Khan
            [ProjectID] => 1
            [ProjectTitle] => Human Resource Management
            [ProjectAbbreviation] => HRM
            [EmployeeBaseSalary] => 45000.00
            [PercentCharged] => 60
            [MonthlySalary] => 45000.00
        )

    [1] => stdClass Object
        (
            [EmployeeID] => 1
            [EmploymentID] => 15
            [EmployeeCode] => E-01
            [EmployeeName] => Yasir Khan
            [ProjectID] => 2
            [ProjectTitle] => telenor
            [ProjectAbbreviation] => ASD
            [EmployeeBaseSalary] => 45000.00
            [PercentCharged] => 40
            [MonthlySalary] => 45000.00
        )

    [2] => stdClass Object
        (
            [EmployeeID] => 9
            [EmploymentID] => 20
            [EmployeeCode] => E-120
            [EmployeeName] => Izhar Ali
            [ProjectID] => 1
            [ProjectTitle] => Human Resource Management
            [ProjectAbbreviation] => HRM
            [EmployeeBaseSalary] => 35000.00
            [PercentCharged] => 50
            [MonthlySalary] => 35000.00
        )

    [3] => stdClass Object
        (
            [EmployeeID] => 9
            [EmploymentID] => 20
            [EmployeeCode] => E-120
            [EmployeeName] => Izhar Ali
            [ProjectID] => 2
            [ProjectTitle] => telenor
            [ProjectAbbreviation] => ASD
            [EmployeeBaseSalary] => 35000.00
            [PercentCharged] => 50
            [MonthlySalary] => 35000.00
        )

    [4] => stdClass Object
        (
            [EmployeeID] => 19
            [EmploymentID] => 32
            [EmployeeCode] => E-014
            [EmployeeName] => Said Ullah
            [ProjectID] => 1
            [ProjectTitle] => Human Resource Management
            [ProjectAbbreviation] => HRM
            [EmployeeBaseSalary] => 20000.00
            [PercentCharged] => 100
            [MonthlySalary] => 54433.00
        )

)

重要部分从我需要为某个员工分配项目的总天数开始。 如果整个月都在开始和结束日期,那么它将是30或31天,但如果项目已在月中分配给员工,那么如何计算总天数呢?

0 个答案:

没有答案