UPDATE queue
SET
status = 'process-$PID'
WHERE ID in (
SELECT ID
FROM queue
WHERE
status = 'pending' AND
target_time > NOW()
ORDER BY
target_time
)
LIMIT $batch_size
I'm trying to do it in one statement so it's thread-safe. Problem is MySQL says "You can't specify target table 'queue' for update in FROM clause."
Is there a way to to this in MySQL? (And stored procedures aren't an option, my host doesn't allow them.)
[edit]
I just tried it using transactions. Start transaction -> select rows -> update rows -> commit transaction. While testing, I ran it without the commit, but the update happened anyway. So what does MySQL do with transactions anyway?