How to get ID of the last updated row in MySQL?

后端 未结 12 2251
北海茫月
北海茫月 2020-11-22 08:10

How do I get the ID of the last updated row in MySQL using PHP?

相关标签:
12条回答
  • 2020-11-22 08:24

    This is the same method as Salman A's answer, but here's the code you actually need to do it.

    First, edit your table so that it will automatically keep track of whenever a row is modified. Remove the last line if you only want to know when a row was initially inserted.

    ALTER TABLE mytable
    ADD lastmodified TIMESTAMP 
        DEFAULT CURRENT_TIMESTAMP 
        ON UPDATE CURRENT_TIMESTAMP;
    

    Then, to find out the last updated row, you can use this code.

    SELECT id FROM mytable ORDER BY lastmodified DESC LIMIT 1;
    

    This code is all lifted from MySQL vs PostgreSQL: Adding a 'Last Modified Time' Column to a Table and MySQL Manual: Sorting Rows. I just assembled it.

    0 讨论(0)
  • 2020-11-22 08:25

    Hm, I am surprised that among the answers I do not see the easiest solution.

    Suppose, item_id is an integer identity column in items table and you update rows with the following statement:

    UPDATE items
    SET qwe = 'qwe'
    WHERE asd = 'asd';
    

    Then, to know the latest affected row right after the statement, you should slightly update the statement into the following:

    UPDATE items
    SET qwe = 'qwe',
        item_id=LAST_INSERT_ID(item_id)
    WHERE asd = 'asd';
    SELECT LAST_INSERT_ID();
    

    If you need to update only really changed row, you would need to add a conditional update of the item_id through the LAST_INSERT_ID checking if the data is going to change in the row.

    0 讨论(0)
  • 2020-11-22 08:25
    SET @uids := "";
    UPDATE myf___ingtable
       SET id = id
       WHERE id < 5
      AND ( SELECT @uids := CONCAT_WS(',', CAST(id AS CHAR CHARACTER SET utf8), @uids) );
    SELECT @uids;
    

    I had to CAST the id (dunno why)... or I cannot get the @uids content (it was a blob) Btw many thanks for Pomyk answer!

    0 讨论(0)
  • 2020-11-22 08:26

    ID of the last updated row is the same ID that you use in the 'updateQuery' to found & update that row. So, just save(call) that ID on anyway you want.

    last_insert_id() depends of the AUTO_INCREMENT, but the last updated ID not.

    0 讨论(0)
  • 2020-11-22 08:27

    Hey, I just needed such a trick - I solved it in a different way, maybe it'll work for you. Note this is not a scalable solution and will be very bad for large data sets.

    Split your query into two parts -

    first, select the ids of the rows you want to update and store them in a temporary table.

    secondly, do the original update with the condition in the update statement changed to where id in temp_table.

    And to ensure concurrency, you need to lock the table before this two steps and then release the lock at the end.

    Again, this works for me, for a query which ends with limit 1, so I don't even use a temp table, but instead simply a variable to store the result of the first select.

    I prefer this method since I know I will always update only one row, and the code is straightforward.

    0 讨论(0)
  • 2020-11-22 08:33

    Further more to the Above Accepted Answer
    For those who were wondering about := & =

    Significant difference between := and =, and that is that := works as a variable-assignment operator everywhere, while = only works that way in SET statements, and is a comparison operator everywhere else.

    So SELECT @var = 1 + 1; will leave @var unchanged and return a boolean (1 or 0 depending on the current value of @var), while SELECT @var := 1 + 1; will change @var to 2, and return 2. [Source]

    0 讨论(0)
提交回复
热议问题