在两个检索日期的表上加入操作

时间:2012-06-25 14:01:15

标签: sql

我的第一次查询,检索datehours来自给定work_details中给定employee number的{​​{1}}。

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 检索date

holy_date

我需要以正确的日期顺序组合两个查询的结果。 我尝试了联合操作,但是得到结果。

我该怎么做?

1 个答案:

答案 0 :(得分:1)

有几种方法可以达到你想要的效果。

这不是记录在案的方式。但这应该有用。

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