Execute a sql query inside IF() function
Learn about Stored Programs.  Write a Procedure that uses IF statements, not functions, to conditionally perform SQL statements.

Then invoke the Stored Procedure with a CALL statement.