选择查询显示第一次和最后一次

时间:2013-04-25 06:58:12

标签: mysql select

这是我目前的表格

enter image description here

是否有可用于显示第一次和最后一次日期的选择查询?我期待的结果如下表所示。

enter image description here

2 个答案:

答案 0 :(得分:3)

SELECT  Day, 
        Date, 
        Department, 
        Name,
        MIN(`Time In`) `Time In`, 
        MAX(`Time Out`) `Time Out`
FROM    tableName
GROUP   BY  Day, Date, Department, Name

答案 1 :(得分:0)

SELECT  Day, 
        Date, 
        Department, 
        Name,
        A.MIN_TIME,
        B.MAX_TIME 
FROM    tableName
LEFT JOIN (SELECT Date, MIN(`Time In`) AS MIN_TIME FROM tableName GROUP BY Date) AS A 
           ON tableName.Date = A.Date
LEFT JOIN (SELECT Date, MAX(`Time Out`) AS MAX_TIME FROM tableName GROUP BY Date) AS B 
     ON tableName.Date = B.Date
GROUP BY Date