UPDATE `tasks`
SET @id := `id`, `state` = 'in_work',
`work_start_time` = NOW(), `worker` = :pid
WHERE `state` = 'wait'
ORDER BY `create_time`
LIMIT 1;
SELECT * FROM `tasks` WHERE `id` = @id;
UPDATE `tasks` SET `state` = 'done' WHERE `id` = @id;
@id := `id`