MySQL automatically adds a column in Select... into outfile...to show the effect of csvleading fields, outfile..csv

Source: Internet
Author: User

MySQL automatically adds a column in Select... into outfile...to show the effect of csvleading fields, outfile..csv
Select... into outfile... it only exports data without field names, so you need to find another way to implement it. Here is a clumsy way to construct a field column by yourself, for example:

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. Use union all to implement the field column and rank it in the first row.

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 ','
Optionally enclosed '"'
Lines terminated by '\ n ';

Run the following command:
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'
-> Fields terminated ','
-> Optionally enclosed '"'
-> Lines terminated by '\ n ';
Query OK, 5 rows affected (0.00 sec)

3. Open the c.csv file to view the effect.

Mysql export csv

In this way, you can:
Select * from mysql table into outfile 'csv table'
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.