Mark Ruiter
Mark Ruiter

Reputation: 37

Only update last row of an user entry

I have this query:

UPDATE uren_registratie SET
                        uren_eind = '".$add_uren_einde."'
                        WHERE user_id = '".$add_user_id."'

The table has on ID column that is updated +1 with each entry.

This is query is updating the fields where user_id matches. But I want only the last entry with that user_id to be updated. How to achieve this, in this query? Or is the only way to use an Select query first to get the latest entry of that user_id?

Upvotes: 0

Views: 30

Answers (1)

GMB
GMB

Reputation: 222582

Assuming that the last entry of a given user is the one with the highest id, you use a correlated subquery, like :

UPDATE uren_registratie
SET uren_eind = '".$add_uren_einde."' 
WHERE user_id = '".$add_user_id."'
AND id = (SELECT MAX(id) FROM uren_registratie WHERE user_id = '".$add_user_id."')

Upvotes: 1

Related Questions