Syntax error when using SELECT INTO OUTFILE
-
27-09-2019 - |
Question
When I run the following query
SELECT a,b,a+b INTO OUTFILE '/tmp/result.txt'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
FROM test_table;
which is straight from the MySQL documentation, I get a syntax error. I don't have a test_table
table and I understand that this statement won't actually work, but it doesn't seem like it should give a syntax error. If I'm getting a syntax error on something straight from the MySQL documentation, what could be going on?
This is the doc I'm looking at (5.1): http://dev.mysql.com/doc/refman/5.1/en/select.html
This is my MySQL version:
mysql Ver 14.14 Distrib 5.1.49, for debian-linux-gnu (i686) using readline 6.1
Solution
This code works for me.
At first I had an Unknown table 'test_table' in my_db
error.
After creating the table CREATE TABLE test_table (a VARCHAR(255), b VARCHAR(255))
it just worked. No syntax error.
Edit:
Running the query from the official mysql
command line client can avoid potential bugs in your GUI client and give more accurate error messages.
OTHER TIPS
Maybe you have to escape some of the characters depending on the language you are using.
php example:
$sql = "SELECT a,b,a+b INTO OUTFILE '/tmp/result.txt'"
. " FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"'"
. " LINES TERMINATED BY '\\n'"
. " FROM test_table;";