MySQL: Why am I getting a syntax error when using the FIELDS ESCAPED BY clause on a select statement
This seems to be a simple syntax issue. Use the below query instead which basically doesn't use FIELDS before ESCAPED by '\\' since it's already specified before TERMINATED by ','.
 SELECT &quot;ansId&quot;,&quot;assmt&quot;,&quot;ques&quot;,&quot;answerCode&quot;,&quot;details&quot; FROM ayanswer
 UNION
 SELECT ansId,assmt,ques,answerCode,details FROM ayanswer
 INTO OUTFILE 'c:/data/test1.csv'
 FIELDS TERMINATED by ','
 OPTIONALLY ENCLOSED BY '&quot;'
 ESCAPED by '\\';

Supporting Resources

13.2.10.1 SELECT ... INTO Statement



TABLE employees ORDER BY lname LIMIT 1000
    INTO OUTFILE '/tmp/employee_data_1.txt'
    FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '&quot;', ESCAPED BY '\'
    LINES TERMINATED BY '\n';



13.2.7 LOAD DATA Statement