Let’s say I have a row with fields a,b,c, d & time_stamp, where a is the unique key and d represents a “deleted” row (which I don’t actually delete, as I want to keep it for an audit trail).
If I modify the row I won’t just UPDATE, becuase I want to keep that audit trail. Instead I will mark the most receent row as being deleted and then insert a new row (I hope Ihave described that clearly).
Now, (how) can I with one SQL staement UPDATE the most recent row (by time_stamp) where a= ?
Maybe something like
UPDATE <table>
SET d="Y"
WHERE a=<some value> AND time_stamp=MAX(time_stamp)
is that correct? Could it be done better? Thanks for any help
Try this:
Just make sure your timestamps are precise enough for the level of activity on the table that there are no 2 rows with the same A and time_stamp values. If you have > 1 row with the same timestamp and A value then you may update more than just the most recent row.