2012-08-28 90 views
0

我也想加入我的查詢,但我卡住在星期天的時間,結合使用日期2個選擇查詢,然後加入2個表

我有一個很基本的,簡單的連接查詢,計算所有的小時的員工,正常時間和加班時間和查詢工作完美,但是當我想參加週日的時候,這一切都亂了套......

全部查詢,而週日時間: -

SELECT 
`empl_attendance`.`employee_id`, 
`employee`.`employee_surname`, 
`employee`.`employee_first_name`, 
`employee`.`employee_second_name`, 
FORMAT((IF(SUM...some basic maths...)),1) AS nt, 
FORMAT((...some basic maths...),1) AS ot, 
FORMAT((...some basic maths...),1) AS total 
FROM 
`empl_attendance` 
WHERE 
(`empl_attendance_date` BETWEEN '$start_date' AND '$end_date') 
INNER JOIN 
`employee` 
ON `empl_attendance`.`employee_id`=`employee`.`employee_id` 
GROUP BY `employee_id` 

但我需要結合下面的查詢,這基本上是計算每個員工的總週日時間。查詢工作對自己很好,我只是努力合併這兩個查詢:

SELECT SUM(`empl_attendance_total`) AS sundays 
FROM `empl_attendance` 
WHERE 
(`empl_attendance_date` BETWEEN '$start_date' AND '$end_date') 
AND 
WEEKDAY(`empl_attendance_date`) > 5 
GROUP BY `employee_id` 

感謝名單

周杰倫

回答

0

你可以用case語句做到這一點:

SELECT ea.`employee_id`, e.`employee_surname`, e.`employee_first_name`, e.`employee_second_name`, 
     FORMAT((IF(SUM...some basic maths...)),1) AS nt, 
     FORMAT((...some basic maths...),1) AS ot, 
     FORMAT((...some basic maths...),1) AS total, 
     sum(case when weekday(ea.empl_attendance_date) > 5 then ea.empl_attendance_total end) as SundayTotal 
FROM empl_attendance ea join 
     employee e 
     ON ea.`employee_id = e.employee_id 
    WHERE ea.empl_attendance_date BETWEEN '$start_date' AND '$end_date' 
    GROUP BY `employee_id` 

我假設以FORMAT開頭的行正在尋找非星期日期,或者至少包括它們,如果這是你想要的。

+0

百萬@Gordon Linoff,它完美的作品。感謝您的幫助,我現在看到我出錯的地方。 –