2013-04-25 65 views
2

我需要將員工姓名分組在一起。我試圖使用「Order By」,但它不適用於空單元格。在MySQL中對相同的行進行分組?

enter image description here

在上面的表格是我目前的表,我要像在底部的表中的結果。這是我目前的選擇查詢。

SELECT 
    DAYNAME(calendar.DATE) AS DAY, 
    DATE_FORMAT(calendar.DATE, '%d %b %Y') AS Date, 
    t2.Department, 
    t2.Name, 
    TIME(t2.time_enter) AS 'Time In', 
    TIME(t2.time_exit) AS 'Time Out' 
FROM 
    calendar 
     LEFT JOIN 
    (SELECT 
     employee_hr_id, 
      time_enter, 
      time_exit, 
      department.NAME As Department, 
      CONCAT(employee.F_NAME, ' ', employee.L_NAME) As Name 
    from 
     entry_log 
    Inner Join employee ON entry_log.EMPLOYEE_HR_ID = employee.HR_ID 
    Inner Join department ON employee.DEPARTMENT_ID = department.ID 
    where 
     CONCAT(employee.F_NAME, ' ', employee.L_NAME) LIKE @NAME) t2 ON date(t2.time_enter) = calendar.date 
where 
    calendar.DATE >= @TIME_ENTER 
     AND calendar.DATE <= @TIME_EXIT 
+0

在第一次加入使用INNER JOIN而不是LEFT JOIN如何? – Devart 2013-04-25 06:40:19

+0

感謝您的建議,但如果我使用INNER JOIN,則不會顯示像星期六或星期天這樣的空行。 – 2013-04-25 06:43:16

回答

2

而不僅僅是CALENDAR表,你應該使用下面的子查詢。

select calendar.DATE,employee.HR_ID,employee.DEPARTMENT_ID 
    from calendar,employee 
    where 
    calendar.DATE >= @TIME_ENTER 
     AND calendar.DATE <= @TIME_EXIT 

在這種情況下,您將獲得DATE和EMPLOYEE的所有可能組合。然後從此表中按DEPARTMENT_ID, HR_ID, DATE排序。

所以,你應該查詢看起來像:

SELECT 
    DAYNAME(NEW_calendar.DATE) AS DAY, 
    DATE_FORMAT(NEW_calendar.DATE, '%d %b %Y') AS Date, 
    t2.Department, 
    t2.Name, 
    TIME(t2.time_enter) AS 'Time In', 
    TIME(t2.time_exit) AS 'Time Out' 
FROM 
    (
     select calendar.DATE as DATE, 
       employee.HR_ID as HR_ID, 
       employee.DEPARTMENT_ID as DEPARTMENT_ID 
      from calendar,employee 
      where 
       calendar.DATE >= @TIME_ENTER 
       AND calendar.DATE <= @TIME_EXIT 
    ) NEW_calendar 
     LEFT JOIN 
    (SELECT 
     employee_hr_id, 
      time_enter, 
      time_exit, 
      department.NAME As Department, 
      CONCAT(employee.F_NAME, ' ', employee.L_NAME) As Name 
    from 
     entry_log 
    Inner Join employee ON entry_log.EMPLOYEE_HR_ID = employee.HR_ID 
    Inner Join department ON employee.DEPARTMENT_ID = department.ID 
    where 
     CONCAT(employee.F_NAME, ' ', employee.L_NAME) LIKE @NAME) t2 

     ON date(t2.time_enter) = NEW_calendar.date 
      and (t2.EMPLOYEE_HR_ID=NEW_calendar.HR_ID) 
where 
    calendar.DATE >= @TIME_ENTER 
     AND calendar.DATE <= @TIME_EXIT 

order by NEW_calendar.DEPARTMENT_ID,NEW_calendar.HR_ID,NEW_calendar.Date 
1

中沒有任何空行來指定要與他們交往什麼的僱員。事實上,他們甚至不應該重複。要走的路是加入三個表(或子查詢):一個爲日期,第二個爲全部(相關)員工,第三個爲實際任務。然後,即使您選擇不打印名稱,您也可以使用所有員工的第二張表來排序行。前兩個表格之間的完整交叉連接爲您提供日期和員工之間的所有所有組合,然後可以將包含日誌條目的組合留在一起。我已創建an example on SQLFiddle。它使用員工名稱而不是ID來匹配事件,並且將t2作爲表格而不是更復雜的子查詢,但該想法應該是乾淨的。這是從該提琴的查詢:

SELECT … 
FROM 
    (calendar, 
    (SELECT DISTINCT Name FROM t2) AS employees) 
    LEFT JOIN t2 ON DATE(t2.time_enter) = calendar.date 
       AND t2.Name = employees.Name 
ORDER BY employees.Name, calendar.date 
+0

謝謝你介紹我的SQLFiddle。 – 2013-05-07 14:42:08

相關問題