Skip to main content

Posts

Showing posts with the label timestamp

Get the ID of latest updated rows and automatically update latest modification timestamp

If you want to get the IDs of the latest updated rows it during a query you can do something like that SET @updatedrows := null; /*we will store here the IDs*/ UPDATE mytable SET coltoupdate = 'mycontent' WHERE myID > 5 AND (SELECT @updatedrows := CONCAT_WS(', ', myID, @updatedrows)); SELECT @updatedrows; /*shows the updated IDs*/ If the rows have already been updated via other queries that you are not aware of you, as far as I know, you must have a column (ex. lastmodified) in the database that must be updated each time that you do modifications. To have *automatic* modifications it's better to use the triggers, if supported by your mysql version. (https://dev.mysql.com/doc/refman/5.0/en/create-trigger.html ) /*adding the lastmodified col to the table*/ ALTER TABLE mytable ADD lastmodified DATETIME NOT NULL; /*creating the trigger*/ CREATE TRIGGER updatelastmodified BEFORE UPDATE ON `mytable ` FOR EACH ROW SET NEW.lastmodified = NOW(); After ...