Hi, I have a problem: I wanna SELECT first row of a table (no ORDER), then DELETE it right away before any other process can select it, then continue with another row. There're many processes that do the same job. Currently, here's what I do: UPDATE `table` SET `selected`=$this_process WHERE `selected`=0 LIMIT 1;// Mark as selected SELECT * FROM `table` WHERE `selected`=$this_process;// Fetch the row with ID DELETE FROM `table` WHERE `id`=$id;// Then delete it Is there any better solution? I'm using MySQL.
Well, You should probably use innodb instead of myisam so you can add locks: http://dev.mysql.com/doc/refman/5.0/en/innodb-transaction-model.html And I suppose you could also use a stored procedure: http://dev.mysql.com/doc/refman/5.1/en/stored-routines.html