2013-07-01 47 views
0

我正在使用內部連接,我想將2個結果組合在一起顯示在singel表中。
我已經創建了查詢來檢查latecoming和earlyleaving現在我希望兩個輸出顯示爲一個singel表。
查詢lateComing:內部連接在mysql中的邏輯

select k.EmpId,Min(k.DateofCheckin) as mindate, 
    case 
    when min(time(DateofCheckin))<[email protected] then 'Ontime' 
    when min(time(DateofCheckin)) between @i and @j then 'Late' 
    when min(time(DateofCheckin)) between @j and @n then 'HalfDay' 
    when DAYOFWEEK(Date(DateofCheckin)) =7 and min(time(DateofCheckin)) >@j then 'HalfDay' 
    else 'Absent' end as LoginStatus 
    from CheckInLogs k Join 
    (select 
    @i:=time(r.LateComer) as LateComing, 
    @j:=time(r.LateComingEndTime) as LateComingEnd, 
    @m:=time(LunchStartTime), 
    @n:=time(LunchEndTime), 
    @o:=time(EarlyLeavingStartTime), 
    @p:=time(EarlyLeavingEndTime), 
    e.EmpId 
    FROM Employees e JOIN Profiles r ON e.LeaveProfile=r.ProfileName) h 
    on k.EmpId=h.EmpId 
    where k.DateofCheckin='in' and 
    DAYOFWEEK(Date(k.DateofCheckin)) != 1 and 
    k.BranchId=1 and date(DateofCheckin) between '2013-05-1' and '2013-05-29' 
    group BY date(k.DateofCheckin),k.EmpId 

查詢EarlyLeaving:

select 
    EmpId,Date(DateofCheckin) as LogoutDate,DateofCheckin,max(time(DateofCheckin)) as LogoutTime, 
    case 
    when max(time(DateofCheckin))>[email protected] then 'Ontime' 
    when DAYOFWEEK(Date(DateofCheckin)) =7 and max(time(DateofCheckin)) < @m 
    then 'HalfDay' 
    when DAYOFWEEK(Date(DateofCheckin)) =7 and max(time(DateofCheckin)) > @m then 'Ontime' 
    when max(time(DateofCheckin)) between @o and @p then 'EarlyLeaving' 
    when max(time(DateofCheckin)) between @m and @o then 'HalfDay' 
    when max(time(DateofCheckin))<[email protected] then 'Absent' 
    else 'Absent' end as LogoutStatus 
    from CheckInLogs k1 Join 
    (select 
    @i:=time(r.LateComingStartTime) as LateComing, 
    @j:=time(r.LateComingEndTime) as LateComingEnd, 
    @m:=time(LunchStartTime), 
    @n:=time(LunchEndTime), 
    @o:=time(EarlyLeavingStartTime), 
    @p:=time(EarlyLeavingEndTime), 
    e.EmpId 
    FROM Employees e JOIN Profiles r ON e.LeaveProfile=r.ProfileName) h1 
    on k1.EmpId=h1.EmpId 
    where k1.DateofCheckin='Out' 
    and k1.BranchId=1 
    and DAYOFWEEK(Date(k1.DateofCheckin)) != 1 and 
    date(k1.DateofCheckin) between '2013-06-1' and '2013-06-29' 
    group BY date(k1.DateofCheckin),k1.EmpId 
+0

如果你有相同的列,你可以使用'UNION'和一個額外的列將是'早'或'遲',這樣你就可以區分結果 – Stephan

+0

或者你可以在emp上的兩個表之間進行連接身份證這是你會在同一排早和晚 – Stephan

+0

內部聯接只會給你他們都遲到和早離開的情況。這是你真正想要的嗎?它看起來像一個工會將是一個更好的主意? – ydaetskcoR

回答

1

下面是如何我會接近查詢爲例,這是一個快速複製和過去的工作,所以你會必須修復語法錯誤等。主要區別在於您只需在查詢開始時獲取一次員工詳細信息,並在兩個當前查詢(員工除外)之間添加一個公共字段即可加入。在這種情況下,我已經使用logoutdate

select * 
from (select 
    @i:=time(r.LateComer) as LateComing, 
    @j:=time(r.LateComingEndTime) as LateComingEnd, 
    @m:=time(LunchStartTime), 
    @n:=time(LunchEndTime), 
    @o:=time(EarlyLeavingStartTime), 
    @p:=time(EarlyLeavingEndTime), 
    e.EmpId 
FROM Employees e JOIN Profiles r ON e.LeaveProfile=r.ProfileName) h 
JOIN (select 
     EmpId, Date(DateofCheckin) as LogoutDate, 
     DateofCheckin,max(time(DateofCheckin)) as LogoutTime, 
     case 
      when max(time(DateofCheckin))>[email protected] then 'Ontime' 
      when DAYOFWEEK(Date(DateofCheckin)) =7 and max(time(DateofCheckin)) < @m 
       then 'HalfDay' 
      when DAYOFWEEK(Date(DateofCheckin)) =7 and max(time(DateofCheckin)) > @m     then 'Ontime' 
      when max(time(DateofCheckin)) between @o and @p then 'EarlyLeaving' 
      when max(time(DateofCheckin)) between @m and @o then 'HalfDay' 
      when max(time(DateofCheckin))<[email protected] then 'Absent' 
     else 'Absent' end as LogoutStatus 
     from CheckInLogs k1  
     where k1.DateofCheckin='Out' 
     and k1.BranchId=1 
     and DAYOFWEEK(Date(k1.DateofCheckin)) != 1 
     group BY date(k1.DateofCheckin), k1.EmpId) out 
ON out.empid = h.empid 
JOIN (select k.EmpId, Date(DateofCheckin) as LogoutDate, 
       Min(k.DateofCheckin) as mindate, 
       case 
        when min(time(DateofCheckin))<[email protected] then 'Ontime' 
        when min(time(DateofCheckin)) between @i and @j then 'Late' 
        when min(time(DateofCheckin)) between @j and @n then 'HalfDay' 
        when DAYOFWEEK(Date(DateofCheckin)) =7 
         and min(time(DateofCheckin)) >@j then 'HalfDay' 
        else 'Absent' end as LoginStatus 
     from CheckInLogs k Join 
     where k.DateofCheckin='in' and 
     DAYOFWEEK(Date(k.DateofCheckin)) != 1 and 
     group BY date(k.DateofCheckin),k.EmpId) in 
ON in.empid = out.empid 
AND in.LogoutDate = out.LogoutDate 

我也會考慮移動casegroup bys到頂級查詢。