在MySQL中合并具有最大ID和最小ID的行

时间:2018-11-24 06:25:39

标签: mysql sql

嗨,我有此表保存入职员工

IO_Id   Employee_Id   EnterDate    EnterTime    ExitDate    ExitTime
--------------------------------------------------------------------------
1          1          11/20/2018   08:00      11/20/2018     09:00
2          1          11/20/2018   09:10      11/20/2018     09:20
3          1          11/20/2018   09:30      11/20/2018     10:00
4          2          11/20/2018   09:30      11/20/2018     11:00
5          1          11/21/2018   10:00      11/21/2018     11:30 

我如何选择任何行都具有针对所有日期和所有雇员的firstEnterTime,fistExitTime,LastEnterTime,LastExitTime的结果

IO_Id   Employee_Id   EnterDate    firstEnterTime       fistExitTime  LastEnterTime    LastExitTime
----------------------------------------------------------------------------------------------------------
1          1          11/20/2018         08:00                 09:00         09:30            10:00
4          2          11/20/2018         09:30                 11:00         09:30            11:00
5          1          11/21/2018         10:00                 11:30         10:00            11:30 

1 个答案:

答案 0 :(得分:0)

您可以在下面尝试使用min()和max()聚合

select min(IO_Id) as IO_Id,Employee_Id,EnterDate as first_EnterDat,
min(EnterTime) as first_EnterTime,min(ExitTime) as fistExitTime,max(EnterTime) as last_EnterTime,max(ExitTime) as lasEtxitTime
from tablename
group by Employee_Id,EnterDate
order by IO_Id