can I use select statement for ifnull function?
Start with this simplification:

CREATE TEMPORARY TABLE t
INSERT INTO t
    SELECT ... -- Keep this; toss the other parts
SELECT FROM t


Then look around for other simplifications.

Read up on INSERT ... ON DUPLICATE KEY UPDATE ... -- it may be the ultimate simplification.

After all that, I may be able to see what you are asking about (IFNULL).