This line:
use test; SELECT `xyflagged`.`Article number`, `xyflagged`.`Flag` INTO outfile 'Test1618' fields terminated by ',' ENCLOSED BY '\"' lines terminated by '\n' FROM xyflagged;
However, this works:
"use test; SELECT * INTO outfile 'Test1618' fields terminated by ',' ENCLOSED BY '\"' lines terminated by '\n' FROM xyflagged;
I've looked at several solutions on SO, such as this and this and several more about escaping strings. Obviously, it has something to do with the space in the column name (I don't have control over that). So I tried various combinations:
use test; SELECT `xyflagged`.`Article number`, `xyflagged`.`Flag` INTO outfile 'Test1618' fields terminated by ',' ENCLOSED BY '\"' lines terminated by '\n' FROM xyflagged;
use test; SELECT `Article number`, `Flag` INTO outfile 'Test1618' fields terminated by ',' ENCLOSED BY '\"' lines terminated by '\n' FROM xyflagged;
use test; SELECT [xyflagged].[Article number], [xyflagged].[Flag] INTO outfile 'Test1618' fields terminated by ',' ENCLOSED BY '\"' lines terminated by '\n' FROM xyflagged;
as suggested by the various answers and the brackets especially appear to have worked for some but not for me.
I've tried it like this:
mysql -u me -paaaaaaaaaaaaaaa -e "use test; SELECT `xyflagged`.`Article number`, `xyflagged`.`Flag` INTO outfile 'Test1618' fields terminated by ',' ENCLOSED BY '\"' lines terminated by '\n' FROM xyflagged";
and also like this:
mysql -u me -paaaaaaaaaaaaaaa << EOF
use test; SELECT `xyflagged`.`Article number`, `xyflagged`.`Flag` INTO outfile 'Test1618' fields terminated by ',' ENCLOSED BY '\"' lines terminated by '\n' FROM xyflagged;
EOF
The error is always:
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ' INTO outfile 'Test1618' fields terminated by ...' at line 1
Maybe it is something basic or obvious, but I think I've tried every combination and I can't make it work.
The other suggested questions/answers that marked this as a duplicate do not address my question. The first one is very general (and ofcourse useful), and already addressed by the first answer. The second one doesn't take into account the variable in the SQL command as noted in my longish comment. Also, the answer is the only complete answer I have found and very different from any other answer I have come across (yet).