INSERT DELAYED IGNORE INTO offers (
user_id,
product_id,
count,
finish_time
) SELECT
user_id,
product_id,
count,
time
FROM
queue
WHERE
state = 'pending'
UPDATE queue
SET state = 'accepted'
WHERE
state != 'accepted'
AND (product_id, user_id) IN (
SELECT
product_id,
user_id
FROM
offers
)