I'm not exactly following you. Can you provide some further details about what exactly you are trying to do?
It might be easier to put the SQL statement in a view in MySQL.
You could also try using multiple data sources - one for each database. If the column names follow a standard convention then that would make blending very easy.
Remember Tableau can probably query the database faster than your own custom SQL, so there's no need to write a lot of code to make it work - you might just be slowing it down.