Created
September 11, 2012 17:26
-
-
Save pixeloution/3700016 to your computer and use it in GitHub Desktop.
a method to update rows and return the IDs of the updated rows
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- the #1 likely use-case for this is when using a mySQL table as queue, which is something | |
-- that should be avoided anyway ... but this allows you to mark items as "in process" | |
-- and return the ids of the items, without using a table lock (mySQL) | |
-- sets up the variable | |
SET @IDS := null; | |
-- should update all the rows and return a list of updated ids in @IDS. Assume: | |
-- status 1 means "to be processed" | |
-- status 2 means "being processed" | |
UPDATE my_queue_table | |
SET status = 2 | |
WHERE client_id = 1 | |
AND status = 1 | |
AND ( SELECT @IDS := CONCAT_WS(',', id, @IDS ) ); | |
-- pull the updated IDS: you end up with a comma separated list of affected IDs | |
SELECT @IDS as ids; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment