mysqldude

INTO OUTFILE and headers

Version compatibility

StepMySQLMariaDB
5.65.78.08.4910.610.1111.411.8
SELECT 'Name',... UNION SELECT * INTO OUTFILE ... FROM t1 (original)
original post
✓✓⚠ 3962⚠ 3962⚠ 3962✗ 1064✗ 1064✗ 1064✗ 1064
Parenthesised second SELECT with INTO OUTFILE and ORDER BY (original)
original post
✓✓⚠ 3962⚠ 3962⚠ 3962✗ 1064✗ 1064✗ 1064✗ 1064
INTO OUTFILE at the end of the whole UNION, sorted with a sort key (modern)
modern rewrite
✓✓✓✓✓⚠ 1287⚠ 1287✓✓
SELECT 'Name',... UNION SELECT * INTO OUTFILE ... FROM t1 (original) original post
MySQL 5.6✓5.7✓8.0⚠ 39628.4⚠ 39629⚠ 3962
MariaDB 10.6✗ 106410.11✗ 106411.4✗ 106411.8✗ 1064
Parenthesised second SELECT with INTO OUTFILE and ORDER BY (original) original post
MySQL 5.6✓5.7✓8.0⚠ 39628.4⚠ 39629⚠ 3962
MariaDB 10.6✗ 106410.11✗ 106411.4✗ 106411.8✗ 1064
INTO OUTFILE at the end of the whole UNION, sorted with a sort key (modern) modern rewrite
MySQL 5.6✓5.7✓8.0✓8.4✓9✓
MariaDB 10.6⚠ 128710.11⚠ 128711.4✓11.8✓
✓works ✓ 1062fails with the error the step demonstrates ⚠works with a warning ≠runs, result differs ✗fails Tested 2026-11-23 on 5.6.51, 5.7.44, 8.0.46, 8.4.13, 9.7.4, 10.6.28, 10.11.19, 11.4.13, 11.8.9
What the warnings and failures say (5)
  • SELECT 'Name',... UNION SELECT * INTO OUTFILE ... FROM t1 (original) (MySQL 8.0, MySQL 8.4, MySQL 9): warning 3962: The INTO clause is deprecated inside query blocks of query expressions and will be removed in a future release. Please move the INTO clause
  • SELECT 'Name',... UNION SELECT * INTO OUTFILE ... FROM t1 (original) (MariaDB 10.6, MariaDB 10.11, MariaDB 11.4, MariaDB 11.8): error 1064: 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 'FROM t1' at line 1
  • Parenthesised second SELECT with INTO OUTFILE and ORDER BY (original) (MySQL 8.0, MySQL 8.4, MySQL 9): warning 3962: The INTO clause is deprecated inside query blocks of query expressions and will be removed in a future release. Please move the INTO clause
  • Parenthesised second SELECT with INTO OUTFILE and ORDER BY (original) (MariaDB 10.6, MariaDB 10.11, MariaDB 11.4, MariaDB 11.8): error 1064: 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 '/tmp/out_demo2.csv' FIELDS TE
  • INTO OUTFILE at the end of the whole UNION, sorted with a sort key (modern) (MariaDB 10.6, MariaDB 10.11): warning 1287: '<select expression> INTO <destination>;' is deprecated and will be removed in a future release. Please use 'SELECT <select list> INTO <dest

Related errors: 1290, 1221, 1064

The original article

First published 2012-05-08. The text below is the original; the table above shows how it behaves on current versions.

This post shows how one can add headers to the contents being exported using the MySQL feature OUTFILE. If we look at a typical case using OUTFILE:

mysql> select * into outfile '/tmp/demo.txt' fields terminated by ',' optionally enclosed by '"' from t1;
Query OK, 8 rows affected (0.00 sec)

mysql> quit
Bye
$ cat /tmp/demo.txt
"ronan4",100,115
"ronan4",155,116
"ronan4",225,250
"ronan4",300,275
"ronan4",325,350
"ronan4",1,2
"ronan4",1,3
"t13",7,6

The data is correct but as headers are missing this may require additional work if we are going to attempt to import this into an Excel file by adding in the headers. Alternatively we can use UNION operation to first add the headers followed by the data.

mysql> select 'Name', 'position', 'points'
    -> union
    -> select * into outfile '/tmp/demo.csv'
    -> fields terminated by ','
    -> optionally enclosed by '"'
    -> from t1;
Query OK, 9 rows affected (0.00 sec)
$ cat /tmp/demo.csv
"Name","position","points"
"ronan4","100","115"
"ronan4","155","116"
"ronan4","225","250"
"ronan4","300","275"
"ronan4","325","350"
"ronan4","1","2"
"ronan4","1","3"
"t13","7","6"

There is one caveat to this. What is we want to order the data. By adding an order by clause we will end up getting the following:

mysql> select 'Name', 'position', 'points'
    -> union
    -> select * into outfile '/tmp/demo.csv'
    -> fields terminated by ','
    -> optionally enclosed by '"'
    -> from t1
    -> order by col2;
Query OK, 9 rows affected (0.00 sec)

mysql> quit
Bye
$ cat /tmp/demo.csv
"ronan4","1","2"
"ronan4","1","3"
"ronan4","100","115"
"ronan4","155","116"
"ronan4","225","250"
"ronan4","300","275"
"ronan4","325","350"
"t13","7","6"
"Name","position","points"

Not exactly what we were looking for. Using the order by in this way causes the header and the data to be merged and sorted. By placing the data SELECT statement within parenthesis will sort the data but leave the headers untouched as follows:

mysql> select 'Name', 'position', 'points'
    -> union
    -> (select * into outfile '/tmp/demo.csv'
    -> fields terminated by ','
    -> optionally enclosed by '"'
    -> from t1
    -> order by col2);
Query OK, 9 rows affected (0.00 sec)

mysql> quit
Bye
$ cat /tmp/demo.csv
"Name","position","points"
"ronan4","100","115"
"ronan4","155","116"
"ronan4","225","250"
"ronan4","300","275"
"ronan4","325","350"
"ronan4","1","2"
"ronan4","1","3"
"t13","7","6"