Q:

Add extra columns for MySQL export to csv?

I have successfully exported my table in MySQL to csv with columns. I used this guy's answer at Include headers when using SELECT INTO OUTFILE?

SELECT 'ColName1', 'ColName2', 'ColName3'
UNION ALL
SELECT ColName1, ColName2, ColName3
FROM YourTable
INTO OUTFILE '/path/outfile'

However, I want to export a query formula as a new column to be added to the csv file. I tried adding an extra calculated column after the second SELECT statement. MySQL gave me an error saying "The used SELECT statements have a different number of columns". 
Example formula: SELECT CAST((ColName1 * ColName2) AS DECIMAL(7,2)) AS ColNameX. I'm not sure where to input it in my export statement.

A:

For the UNION to work you must have the same number of columns and they must be of the same type. As you creating the header row as text then all of your columns in the second query must also be text. Like so:
SELECT 'ColName1', 'ColName2', 'ColName3', 'New Column'
UNION ALL
SELECT 
  ColName1
  ,ColName2
  ,ColName3
  ,CAST(CAST((ColName1 * ColName2) AS DEC(5,2)) AS CHAR)
FROM YourTable
INTO OUTFILE '/path/outfile'

