Export database record as CSV file - mysql

Is there any way to export database records as CSV file from Mysql Workbench by using command line syntax?

SELECT orderNumber, status, orderDate, requiredDate, comments
FROM orders
WHERE status = 'Cancelled'
INTO OUTFILE 'C:/tmp/cancelled_orders.csv'
FIELDS ENCLOSED BY '"' TERMINATED BY ';' ESCAPED BY '"'
LINES TERMINATED BY '\r\n';
more information here:
http://www.mysqltutorial.org/mysql-export-table-to-csv/

MySQL Workbench comes with a table data export function, which supports csv and json formats. You can start it from the context menu in the schema tree:
The simple wizard then allows you to select the output data type:
Similarly, there's the table data import wizard that can read csv and json data.

Related

Export MySQL Table to CSV file through Batch File

I need to export my MySQL tables to CSV files with a batch file. Is there any option like that?
I am getting many ETL references in Google search, but is there any command line arguments, which can be used in batch file so that I can export the data in CSV format with headers?
There is an URL export mysql table to csv file using batch script regarding this, but not clear, what to write in the batch file.
Can anyone please help me?
SELECT * FROM firstdb.user
INTO OUTFILE 'C:/ProgramData/MySQL/MySQL Server 8.0/Uploads/employee_backup.csv'
FIELDS ENCLOSED BY '"'
TERMINATED BY ';'
ESCAPED BY '"'
LINES TERMINATED BY '\r\n';

How to load json from csv of postgres to mysql

I using postgres and mysql . Now i want migrate data from postgres to mysql.
I export data from postgres to csv and then reimport it to mysql.
My csv export from postgres with name : test.csv . Column data type json of postgres.
Test.csv
id,name,data
"1","someName","{""email"": ""a#gmail.com"", ""country"": ""US""}"
And in mysql i using command reimport to mysql with same column
LOAD DATA LOCAL INFILE '/tmp/test.csv' INTO TABLE someTable FIELDS TERMINATED BY ',' TERMINATED BY '\n' IGNORE 1 LINES;
When i import it import success but column data is null . I don't know why
If i using commmand
LOAD DATA LOCAL INFILE '/tmp/test.csv' INTO TABLE someTable fields terminated by ',' optionally enclosed by '\"' escaped by '\'' lines terminated by '\n' IGNORE 1 LINES;
MYSQL thow me exception :
Invalid JSON text: "The document root must not be followed by other values." at position 4 in value for column 'someTable.data'.
How to import from csv with json column to mysql. Please help . Thanks you

Export JSON data from MySQL table to CSV

I used the following command to export some fields of MySQL table including a JSON field(attributes) into CSV file:
SELECT name, attributes, product_url FROM products INTO OUTFILE '/var/lib/mysql-files/toys.csv' FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n';
But, I get each key-value pair of attributes(JSON field) in separate columns.
How to get all those key-values(attributes column of MySQL table) in a single column of CSV file?
I found a solution that was enough to get my job done. I exported those fields into TSV instead of CSV using following slightly modified command:
SELECT name, attributes, product_url FROM products INTO OUTFILE '/var/lib/mysql-files/toys.tsv' FIELDS TERMINATED BY '\t' LINES TERMINATED BY '\n';
Still, if anyone has an exact solution to the problem, that would be greatly appreciated.

How to import xls file in to MySQL table

I'm using ubuntu 14.04 operating system and I want to import excel sheet(.xls) in to database.
I am using phpmyadmin mysql administration tool. If any one can give me the commands to import xls to mysql I would be grateful.
Import CSV file in to MySQL Table
Change file type in to CSV
Create table according to excel column names
Place the csv file in /var/lib/mysql
Open phpmyadmin sql command pane:
Execute this sql command
LOAD DATA INFILE 'testlist.csv'
INTO TABLE TEST
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS;
Use the load data capability. See
http://dev.mysql.com/doc/refman/5.1/en/load-data.html
Export CSV file in to MySQL Table
SELECT ... FROM ... WHERE ...
INTO OUTFILE 'testlist.csv'
FIELDS TERMINATED BY ','
Here is an example that produces a file in the comma-separated values (CSV) format used by many programs:
SELECT * INTO OUTFILE '/tmp/result.txt'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
FROM test_table;

Export a SQL database into a CSV file and use it with WEKA

How can I export a query result from a .sql database into a .csv file? I tried with
SELECT *
FROM players
INTO OUTFILE 'players.csv'
FIELDS TERMINATED BY ','
LINES TERMINATED BY ';';`
and my .csv file is something like:
p1,1,2,3
p2,1,4,5
But they are not in saparated columns, all are in 1 column.
I tried to create a .csv file by myself just to try WEKA, something like:
p1 1 2 3
p2 1 4 5
But WEKA recognizes p1 1 2 3 as a single attribute.
So: how can I export correctly a table from a sql db to a csv file? And how can I use it with WEKA?
EDIT
Is it possible to add a header line with columns names? I have read something online but I am not able.
I'm using MySQL 5.5.
You need to inform weka about format of your csv file. You can do so while opening csv file and invoking options dialog, see below images. Also you need to terminate your lines with "\n" instead of ";".
Try this.
First of all convert the .sql file to .csv format. For eg:
SELECT orderNumber, status, orderDate, requiredDate, comments
FROM orders
INTO OUTFILE 'C:/tmp/cancelled_orders.csv'
FIELDS ENCLOSED BY '"' TERMINATED BY ',' ESCAPED BY '"'
LINES TERMINATED BY '\r\n';
NOTE: Weka tool need the .csv file`s attributes terminated by a comma. If you use semi colon, it wont work. You will get an exception.
Now this will convert the .sql format to .csv format which you can find in the C:/tmp/cancelled_orders.csv location.
Finally start the weka tool and fetch this data set to it.
NOTE : Make the "Files of Type" as "CSV data files".
Then it will work without giving any exception. It works.