Search code examples
mysqlsqlsqlyog

how do i get the last inserted column in sqlyog database without using top or last function?


I have a table named tbltimpunch that has the following columns:

  • employeeID (not primary key/not auto inc)
  • dateoftime (time of day)
  • timein (the time input by the employee)
  • timeout (the time of clockout)

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?


Solution

  • 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.

    Updated

    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