I'm trying to select the last updated rows' id of my table, then I find this question How to get ID of the last updated row in MySQL?.
SET @uids := null;
UPDATE footable
SET foo = 'bar'
WHERE fooid > 5
AND ( SELECT @uids := CONCAT_WS(',', fooid, @uids) );
SELECT @uids;
The solution works, but I don't understand why I have to set @uids to null instead of empty string. Why this statement only select affected rows instead of matched rows even with a limit clause appended. Is this a MySQL defined behavior? Which page of the MySQL manual shoud I look up?
mysql> select * from transaction_test;
+----+--------+------------+
| id | value1 | value2 |
+----+--------+------------+
| 1 | 1 | 1460600984 |
| 2 | 2 | 1460598960 |
+----+--------+------------+
2 rows in set (0.00 sec)
mysql> set @ids=null;update transaction_test set value2=unix_timestamp(now()) where 1=1 and ( SELECT @ids:=concat_ws(',',@ids,value1) ) limit 1;select @ids;
Query OK, 0 rows affected (0.00 sec)
Query OK, 1 row affected (0.05 sec)
Rows matched: 1 Changed: 1 Warnings: 0
+------+
| @ids |
+------+
| 1 |
+------+
1 row in set (0.00 sec)
mysql> set @ids='';update transaction_test set value2=unix_timestamp(now()) where 1=1 and ( SELECT @ids:=concat_ws(',',@ids,value1) ) limit 1;select @ids;
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Rows matched: 0 Changed: 0 Warnings: 0
+------+
| @ids |
+------+
| ,1,2 |
+------+
1 row in set (0.00 sec)