CREATE TEMPORARY TABLE pays_doubles AS SELECT mid,id FROM pays WHERE type=10 AND category=110 AND time>unix_timestamp('2013-08-31 23:59:00') GROUP BY mid having count(*)>1;UPDATE users LEFT JOIN pays_doubles ON users.id=pays_doubles.mid SET users.balance=users.balance+pays_doubles.cash;DELETE FROM pays LEFT JOIN pays_doubles ON pays.id=pays_doubles.id;