Jojo George
Jojo George

Reputation: 159

Join operation on two tables retrieving dates

My first query, retrieving date and hours worked from work_details of a given employee number in a given date.

SELECT date,
       SEC_TO_TIME( SUM( TIME_TO_SEC( `total_hours` ) ) ) AS total
FROM `work_details`
WHERE employee_id='28'
  and date between  '2012-02-01'
  and '2012-02-29'
GROUP BY DATE ORDER BY DATE

and the Second query retrieving date from table holy_date:

SELECT holy_date
from holiday
where holy_date between '2012-02-01' and '2012-02-29'

I need to combine results of the two queries in the correct date order. I tried union operation,but dint get result.

How can I do it?

Upvotes: 0

Views: 214

Answers (1)

nawfal
nawfal

Reputation: 73183

There are a few ways to achieve what you want.

This is not the documented way of doing it. But this should work.

SELECT   date, total
FROM 
         (
          SELECT   date, SEC_TO_TIME( SUM( TIME_TO_SEC( `total_hours` ) ) ) AS total 
          FROM     `work_details` 
          WHERE    employee_id='28' AND date BETWEEN  '2012-02-01' AND '2012-02-29' 
          GROUP BY date 

          UNION ALL

          (  
           SELECT holy_date AS date, NULL AS total 
           FROM   holiday 
           WHERE holy_date BETWEEN '2012-02-01' AND '2012-02-29'
          )
         ) AS t
GROUP BY date
ORDER BY date

Upvotes: 1

Related Questions