I have a table named tbltimpunch
that has the following columns:
It can be created with the following mysql:
CREATE TABLE tbltimpunch(
`employeeID` varchar(10),
`dateoftime` datetime,
`timein` time,
`timeout` time
);
How can I get the last timein
or timeout
input by the employee?
How about this?
SELECT employeeID, MAX(timein) AS lastIn, MAX(timeout) AS lastOut
FROM tbltimpunch
GROUP BY employeeID;
This'll return a result set with a row for every employee (that has data in the table), along with their last timein and timeout values.
This solution might be closer to what you're looking for:
SELECT t1.* FROM tbltimpunch t1
LEFT JOIN tbltimpunch t2 ON t1.employeeID=t2.employeeID
GROUP BY t1.employeeID, t1.dateoftime
HAVING t1.dateoftime=MAX(t2.dateoftime);
This will return the one row for each employee that has the latest dateoftime value.
Try the fiddle, here: http://sqlfiddle.com/#!2/ca4f3/1/0