How to export data to csv in mysql without the header? - mysql

MySQL has the functionality to export the data to a CSV file with following statement
SELECT
*
FROM
person
INTO OUTFILE 'person.csv'
FIELDS ENCLOSED BY '"'
TERMINATED BY ','
ESCAPED BY '"'
LINES TERMINATED BY '\r\n';
But by default it will has the column name as the CSV header, so how to remove the header in this statement?
I reviewed the mysql reference but seems these is no such info.

2 options are provided in this post : How can I suppress column header output for a single SQL statement?)
1 : invoke mysql with -N flag will skip all column headers
2 : fake it
select column1 as '', column2 as '' from some_table;

After complete the csv file, seems for the huge records, the big csv file has no header, so if you want to add the header, following:
Several users asked about including headers, i.e., column names or variable names, in the "INTO OUTFILE" syntax.
One approach is to use the "--column-names" option in the mysql invocation:
mysql --column-names -e 'SELECT * FROM mysql.user' > test.dat
(This creates a tab-delimited file test.dat with column names in the first row followed by the query results.)

SELECT
*
FROM
person
INTO OUTFILE 'person.csv'
FIELDS ENCLOSED BY '"'
TERMINATED BY ','
ESCAPED BY '"'
LINES TERMINATED BY '\r\n';
change FIELDS ENCLOSED BY '"' line
with OPTIONALLY ENCLOSED BY '"'

Related

How to export a CSV file from the MySQL command line

I have a MySQL database containing a single table which is used as a temporary storage point for manipulating and querying data sets and is periodically deleted and replaced with new data. What I would like to be able to do is export the data from the MySQL command line to use for other purposes. I am using the XAMPP Apache server package with phpMyAdmin on Windows 10.
The issue I am having is the INTO OUTFILE syntax I am using returns an error relating to '\n'. Below is an example of the syntax:
SELECT *
FROM tablename
WHERE work_complete = 'Yes'
INTO OUTFILE 'C:\file path for the file to be exported to\file_name.csv'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY "''"
ESCAPED BY '\'
LINES TERMINATED BY '\n';
I have spent some time researching this without any luck, I have even tried using
LINES TERMINATED BY '\r\n'
but the error remained the same
ERROR: Unknown command '\''.
-> LINES TERMINATED BY '\r\n';
If anyone could provide any tips that would be greatly appreciated.
Use this
SELECT *
FROM tablename
#WHERE work_complete = 'Yes'
INTO OUTFILE 'C:\file path for the file to be exported to\file_name.csv'
FIELDS ENCLOSED BY '"'
TERMINATED BY ';'
ESCAPED BY '"'
LINES TERMINATED BY '\r\n';
I used some other syntax as you and deleted the OPTIONALLY which mysql doesn't like at that place
The problem is not in LINES TERMINATED BY but in ESCAPED BY.
This:
ESCAPED BY '\'
Is invalid syntax, because the backslash is interpreted as an escape character for the following quote. A decent text editor should let you see that.
You need to escape the backslash, like so:
ESCAPED BY '\\'
Alternatively, you can also use '\b':
ESCAPED BY '\b'
Another probem is that OPTIONALLY ENCLOSED accepts only a single character, while you are giving it two single quotes.
In your query:
SELECT * FROM tablename WHERE work_complete = 'Yes'
INTO OUTFILE 'C:\file path for the file to be exported to\file_name.csv'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY "'" ESCAPED BY '\\'
LINES TERMINATED BY '\n'

Mysql How to outfile in csv format, with sheets options?

I want to export data from mysql into a csv. There are many tables so I want a csv file with many sheets. How it can be done?
I suppose somethings like:
SELECT *
FROM product
WHERE active = 1
INTO OUTFILE '/root/tmp/data.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n';
SELECT *
FROM member
WHERE active = 1
INTO OUTFILE '/root/tmp/data.csv' // using the same .csv
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n';
etc...
You cannot append to an existing file using INTO OUTFILE.
https://dev.mysql.com/doc/refman/5.7/en/select-into.html says:
file_name cannot be an existing file, which among other things prevents files such as /etc/passwd and database tables from being destroyed.
So you'll have to output to a different file per table, and then concatenate them together yourself (that is, not using SQL).

Not able to import price column properly in MySQL from a CSV file

I've a csv file containing data in this format:
SATURN,6459,"50,486",27184
I'm using this command to import this file into the table:
LOAD DATA LOCAL INFILE 'D:\\temp.csv' INTO TABLE `test`.`tmp` FIELDS ESCAPED BY '\\'
TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n'
(`description`,`file_no`,`net`,`run_no`);
all the fields are being imported correctly but the net column always having the data like
50.00 // it should be 50,486
Data type of this column is Decimal(10,2). I've also tried Numeric(10,2) but no luck. Any help is highly appreciated. Thanks a lot in advance.
Please have a try with this one:
LOAD DATA LOCAL INFILE 'D:\\temp.csv' INTO TABLE `test`.`tmp` FIELDS ESCAPED BY '\\'
TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n'
(`description`, `file_no`, #a_variable, `run_no`)
SET `net` = REPLACE(#a_variable, ',', '');
MySQL cannot deal with commas as 1000 separators when inserting numbers. You need to parse out these commas before loading.
If you need the numbers formatted this way when querying, use FORMAT(). For examples see Apply comma to number field in MySQL

Inserting CSV into mysql

this may just be the dumbest question I have asked yet. But I can not figure this out. I am trying to import a CSV into my mysql. But I can not get these areas correct:
Columns separated with:
Columns enclosed with:
Columns escaped with:
Lines terminated with:
Here is an example row in mycsv:
AA|Armed Forces Americas|US|United States
what would I place in each one?
If you have table that match the column configuration of the file what you do is:
LOAD DATA INFILE '<full file path>`
INTO TABLE <tbl_name>
FIELDS TERMINATED BY '|' LINES TERMINATED BY '\n';
If some of the fields may contain | then these fields should be enclosed by " and then the last clause becomes:
FIELDS TERMINATED BY '|' ENCLOSED BY '"' TERMINATED BY '\n'

Using MySQL OUTFILE with non-simple data

I've been using the following command to expert mysql data to a csv file.
SELECT * INTO OUTFILE 'output.csv' FIELDS TERMINATED BY '\t' LINES TERMINATED BY '\n' FROM table1;
It works for simple tables with simple data. However, if the table contains html tags, double quotes, single quotes, ascii characters etc, it does not work propertly, i.e. it will put tabs and new lines in incorrect places, breaking up data where it shouldn't. How can the sql script above be improved to export data with html?
I have tried SELECT...INTO OUTFILE statement, and then LOAD DATA INFILE statement, everything is OK, the HTML text was exported/imported without any mistakes (on MySQL 5.5).
Try to add ENCLOSED BY option, it should help you, e.g. -
SELECT *
INTO OUTFILE 'output.csv'
FIELDS TERMINATED BY '\t' ENCLOSED BY '"'
LINES TERMINATED BY '\n'
FROM
table1;
LOAD DATA INFILE 'output.csv'
INTO TABLE table1
FIELDS TERMINATED BY '\t' ENCLOSED BY '"'
LINES TERMINATED BY '\n';