From MySQL Documentation - 14.2.7.9 How to Cope with Deadlocks (highlight added):
When modifying multiple tables within a transaction, or different sets of rows in the same table, do those operations in a consistent order each time. Then transactions form well-defined queues and do not deadlock. For example, organize database operations into functions within your application, or call stored routines, rather than coding multiple similar sequences of INSERT, UPDATE, and DELETE statements in different places.
If this is the cause, adding an ORDER BY
clause to Query 1 should help.