This is my query:
INSERT INTO table (value) VALUES (value) ON DUPLICATE KEY UPDATE value=value
With mysql_insert_id()
I get the new inserted ID, but how to get the ID of row updated?
I tried mysql_insert_id()
but, for updated row, I get 0.
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();
Go for After Insert/update trigger. It will always give you the last inserted/updated record ,from the dynamic table called Inserted... Query: Select * from Inserted.
You can use a rowid to locate the internal record number that is associated with a row in a table. Rows in fragmented tables do not automatically contain the rowid column. It is recommended that you use primary keys as a method of access in your applications rather than rowids.
We can use the ORDER BY statement and LIMT clause to extract the last data. The basic idea is to sort the sort the table in descending order and then we will limit the number of rows to 1. In this way, we will get the output as the last row of the table.
MySQL ON DUPLICATE KEY - last insert id?
"Check this page out: http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html At the bottom of the page they explain how you can make LAST_INSERT_ID meaningful for updates by passing an expression to that MySQL function."
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With