Combine what Jan suggested with the mySQL outfile (just name it .sql).
In your select, where you would include the SQL commands as literals.
If you need to resolve some formatting issues with the output check the mySQL manual. Search on mySQL+outfile+syntax
you should be able to find it pretty quick.
As an alternative, if all your doing is inserting data into a table based on SQL select (which is likely in the same schema)... you really only need a single statement (though this may not fit your criteria):
INSERT INTO myTable (column1, column2, ...)
SELECT column3, column4, ...
FROM myOthertable