Is there an efficient way to get the list of affected row IDs (not the # of affected rows via PHP's mysql_affected_rows(), but the actual row ids that were affected) from an UPDATE or DELETE query in mysql?
In postgresql, there is a RETURNING clause within UPDATE/DELETE queries that can be used to specify values from the affected rows that are returned.
In mysql, the 'brute force' way of getting the affected rows seem to be: 1. Acquire READ LOCK. 2. SELECT to with the WHERE condition of the UPDATE/DELETE query to get the affected row ids. 3. UPDATE/DELETE. 4. RELEASE LOCK.
The above way seems very inefficient. Is there a more efficient way to get the affected row ids in mysql?
try this, it will return the updated ids as "1,2,3....":
You can create a Trigger
the following code creates a trigger on a table named
mytable
which has a fieldid
notice that
OLD
refers to deleted rowonce you created a trigger on a table you can use it as follows:
this will return deleted IDs each preceded by a comma in a string
sql mysql php