in the sample CSV exampleSelect ... into outfile ... is not with the field name, only export data, so you need to think of another way to implement, here is a stupid trick, you construct a field column, the example is as follows:
1. Create Test table Data
CREATE TABLE test.c SELECT 1 as pid,1 as item,15.0 as WGT UNION all
SELECT 1 as pid,2 as item,20.0 as WGT UNION all
SELECT 1 as pid,3 as item,30.0 as WGT UNION all
SELECT 1 as pid,4 as item,29.0 as WGT;
SELECT * from test.c;
mysql> SELECT * from test.c;
+-----+------+------+
| pid | item | wgt |
+-----+------+------+
| 1 | 1 | 15.0 |
| 1 | 2 | 20.0 |
| 1 | 3 | 30.0 |
| 1 | 4 | 29.0 |
+-----+------+------+
4 ROWS in SET (0.00 sec)
2, the field column is implemented through union all, and the first row is ranked
SELECT * FROM (
SELECT ' pid ' as PID, ' item ' as item, ' WGT ' as Wgt
UNION All
SELECT * from test.c
) A into OUTFILE '/tmp/c.csv '
Fields TERMINATED by ', '
optionally enclosed by ' "'
LINES TERMINATED by ' \ n ';
EXECUTE as follows:
mysql> SELECT * FROM (
--SELECT ' pid ' as PID, ' item ' as item, ' WGT ' as Wgt
UNION All
SELECT * from test.c
A into OUTFILE '/tmp/c.csv '
--TERMINATED by ', '
- Optionally enclosed by ' "'
- LINES TERMINATED by ' \ n ';
Query OK, 5 rows Affected (0.00 sec)
3, go to open the C.csv file to see the effect
MySQL by self-increment a column in the Select ... into outfile ... The results of CSV export with fields are implemented inside