In MySQL, is it possible to update the selected records on the same query?
For eg., If the query
SELECT *
FROM `table`
WHERE field = "value"
LIMIT 0,2
Return two rows, then on the same query, i need to increment the table’s count field by 1. Is it possible?
It’s not possible. The verb
SELECTonly retrieves data (without modifying it); and the verbUPDATEonly modifies data (without retrieving it). There is no MySQL verb that will perform both actions. You will have to use two separate statements.However, those two statements can be encapsulated within a transaction (if supported by your storage engine) to ensure that they are conducted atomically and/or could be invoked from within a stored procedure to simplify the command that must be issued by your client. Combining the two one would have:
Then your client need merely do: