pixeloution
9/11/2012 - 5:26 PM

a method to update rows and return the IDs of the updated rows

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;