views:

26

answers:

1

Hello

I was wondering if it's possible to get the resultset of the select query from my transaction. Mysql returns that the table was updated, but returned 0 rows.

This is the transaction:

START TRANSACTION;
SELECT *, @A:=id FROM mailer_log LIMIT 0,10;
UPDATE mailer_log SET picked=1 WHERE id=@A;
COMMIT;
A: 

If you do these step by step you have the resultset after step 2.

If you'd like the resultset of the modified records, you'll have to do the query again.

This is not as bad as it sounds, since all data will be in memory anyhow (unless you have millions of records with the same id of course) since you just retrieved and updated them.

Example in perl :

$dbh->begin_work;
$ary_ref  = $dbh->selectall_arrayref("SELECT * FROM mailer_log WHERE id = '$a' LIMIT 0,10");
# ... do something interesting with the result set
$dbh->do("UPDATE mailer_log SET picked=1 WHERE id='$a'");
$dbh->commit;
Peter Tillemans
Well, I really need the results of the first query, because this transaction can be called by different requests at the same time... That's why I put it in a single transaction.
You can still run it in one transaction. Just do the BEGIN and SELECT, get the resultset and do the UPDATE and COMMIT.As long as the time between BEGIN and COMMIT is reasonably short, you can do whatever you want or need in between. (Just check that autocommit is disabled in your driver)
Peter Tillemans
Okay, but I still don't get it :) Can you edit my code with your solution?First time I'm into mysql transactions...
I added an example in perl. You did not indicate which environment you are using so I improvised. but the meaning should be clear.
Peter Tillemans