MySQL by self-increment a column in the Select ... into outfile ... The results of CSV export with fields are implemented inside

Source: Internet
Author: User

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

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.