How to get affected rows (not just the number of rows affected) after update statement in mysql.
@@ROWCOUNT – Get the Number of Rows Affected by the Last Statement in SQL Server. In SQL Server, you can use the @@ROWCOUNT system function to return the number of rows affected by the last T-SQL statement. For example, if a query returns 4 rows, @@ROWCOUNT will return 4.
MySQL ROW_COUNT() can be used to get the total number of rows affected by MySQL query. To illustrate it we are creating a procedure with the help of which we can insert records in a table and it will show us how many rows have been affected.
In MySQL the ROW_COUNT() function is used to return the number of rows affected by the previous SQL statement. If the previous statement was not one that could potentially change data rows or you can say, it wasn't an INSERT, UPDATE, DELETE or other such statement this function will return -1.
The affected_rows / mysqli_affected_rows() function returns the number of affected rows in the previous SELECT, INSERT, UPDATE, REPLACE, or DELETE query.
mysql_info — Get information about the most recent query
$recent = mysql_info();
Useful implementation Example
If you want the actual rows and not the amount of affected rows, simply fetch them before doing the update.
Afterwards you can compare update values with selected values and filter them by difference.
Example with CodeIgniter:
$arr_where = array('date >=' => date('Y-m-d H:i:s'));
$query = $this->db->get_where('table', $arr_where);
$arr_update = array('status' => 'TRUE');
if ($this->db->update('table', $arr_update, $arr_where)) {
foreach($query->result() as $row)
foreach(array_keys($arr_update) as $h)
if ($row->$h != $arr_update[$h])
echo "This row ({$row->id}) had it's {$h} changed!<br />";
}
Sorry for supplying solution in CodeIgniter, but I found it to be the simpliest example.
To see what the $this->db
functions do, see Active Records.
You could try using:
SET @uids := '';
UPDATE <table>
SET <column1> = <value>
WHERE <column2> = <value>
AND ( SELECT @uids := CONCAT_WS(',', @uids, id) );
SELECT TRIM(LEADING ',' FROM @uids);
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