1

I have a check-in / check-out system which writes for every check-in a new row in my table and updates the table when the person checks out (like checkedout = 1)

Now I'm making a new site that always shows the newest checked in person. I do it by polling and storing the highest ID on a variable on that page. In the polling I search for entries > the id i stored. It's working good so far.

But now I want to extend it and show either the latest checked in person OR the latest checked out person. How can I get the last "updated" row in my table?

Florian Sauerwein
  • 445
  • 1
  • 5
  • 19

3 Answers3

2

You can add a column for ex. date_checked of type datetime and update it whenever something happens.

After that just select by that column.

Vasil Rashkov
  • 1,818
  • 15
  • 27
  • Right! Sometimes the easiest solutions are hardest to think of :D thanks! – Florian Sauerwein Mar 02 '16 at 09:57
  • Create this field with `date_checked TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP`, and MySQL will do almost all the work for you. – syck Mar 02 '16 at 10:27
2

use mysql function called mysqli_insert_id() which will give you last inserted primary key value of the table

Om R Kattimani
  • 105
  • 1
  • 9
1

try this:

SET @update_id := 0;
UPDATE some_table SET column_name = 'value', id = (SELECT @update_id := id)
WHERE some_other_column = 'blah' LIMIT 1; 
SELECT @update_id;

More Click Here

Community
  • 1
  • 1
Arnab
  • 4,216
  • 2
  • 28
  • 50