delete from `transaction_history` where transaction_code in( select trans_code from `transaction` where DATEDIFF(CURDATE(), created_at)>3 and trans_step<6); update cars set status=3 where code in (select car_code from `transaction` where DATEDIFF(CURDATE(), created_at)>3 and trans_step<6 ); delete from `transaction` where DATEDIFF(CURDATE(), created_at)>3 and trans_step<6;