Skip to content

Instantly share code, notes, and snippets.

@pixeloution
Created September 11, 2012 17:26
Show Gist options
  • Save pixeloution/3700016 to your computer and use it in GitHub Desktop.
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
-- 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