Since none of the columns from projetstaches
are referenced in the UPDATE
, and the most recent record per timUserId
can be determined by ordering timesheet
, the JOIN
is unnecessary, and indeed all the other tables are unnecessary. Using ORDER BY
and LIMIT
, you can target the specific record.
Sort by timCreateDate DESC
and by timId
for the cases where two share the same date since it is a DATE
column rather than a DATETIME
.
UPDATE timesheets
SET timVerified = :timVerified
WHERE timUserId = 7
ORDER BY
timCreatedDate DESC,
timId DESC
# Crucially, LIMIT 1 to target one record only (the first in the sort)
LIMIT 1
Reference MySQL UPDATE
syntax reference
The query as printed above can be placed directly into your PDO
code with the execute()
call and its array parameter defined as you already have it.
Note: I would recommend changing the timCreatedDate
from a DATE
type to DATETIME
so that you may target it with more granularity. If you did that, you may not need to use timId DESC
in the ORDER BY
.