Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get ID row of updated row?

Tags:

php

mysql

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.

like image 917
Leonardo Avatar asked Feb 16 '11 13:02

Leonardo


People also ask

Which method is used to get the row ID of the last modified row?

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();

How do I find the last updated record ID in SQL?

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.

How do I find the row ID in SQL?

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.

How do I get the latest row in SQL?

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.


1 Answers

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."

like image 138
Luka Avatar answered Nov 15 '22 13:11

Luka