How to cache query for re-use in following UNION of queries?
As you say, temporary tables are only visible within the session, and materialized views are not available in MySQL. There are several options (including the ones you have mentioned). I will name your original query as SELECT ...:


The WITH Oracle keyword is equivalent to CREATE TEMPORARY TABLE:

CREATE TEMPORARY TABLE savedquery AS SELECT ...; -- you execute this for every connection
SELECT id FROM savedquery WHERE .....
UNION
SELECT id FROM savedquery WHERE .....;


You will have to create it for every connection.
A VIEW is shared among connections, but it is not an actual table- it can be executed in two ways, by creating a temporary table at the beginning of the execution or by executing the corresponding subquery:

CREATE VIEW savedquery 
ALGORITHM = TEMPTABLE  -- you probably want to use temptable algorithm
AS SELECT ...;         -- you execute this only once

SELECT id FROM savedquery WHERE .....
UNION
SELECT id FROM savedquery WHERE .....;


If you were using 5.6, the subquery algorithm may be better, but this is without knowing the actual query.
As materialized views are not an option, you can emulate them by creating a real table, that will be shared among connections:

CREATE TABLE savedquery AS SELECT ...;
SELECT id FROM savedquery WHERE .....
UNION
SELECT id FROM savedquery WHERE .....;


The problem with this is that it is not automatically updated (you have to do that with triggers or any other way); the positive thing is that you can add indexes (unlike views).


The problem is that your original query and your derived query are probably not going to be very efficient, and you probably could modify it to perform better, but that is another question...