Howto mysql multiple outfiles into a single compressed zip - mysql

I am running Xubuntu 16.04 and MariaDB with MySQL.
These 3 files (customers.csv, items.csv, invoices.csv) are created by
MySQL INTO OUTFILE commands as seen below.
These 3 files needs to go into a single zip file (report.zip).
The 3 files (customers.csv, items.csv, invoices.csv) don't need to be saved permanently as they only serve as temp files so that they can be packed into the zip file.
My sample MySQL outfile commands:
SELECT customer_id, firstname, surname FROM customers
INTO OUTFILE '/tmp/customers.csv'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n';
SELECT item_id, itemname, item_plu FROM items
INTO OUTFILE '/tmp/items.csv'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n';
SELECT invoice_id, invoice_total FROM invoices
INTO OUTFILE '/tmp/invoices.csv'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n';
Question
Do you guys know a MySQL command in which the INTO OUTFILE files go directly into a zip file without being stored as 3 additional separate files onto the disk?

I just read your question and I found the answer in the same time while I was looking for the same feature. So I share what I found :
Unfortunately, it seems that MySQL doesnt support direct output compression, this feature is requested but not implemented yet :
This is the link
The best way in my knowledge to do that would be to do it in two steps :
Output
Zip with command line
Joffrey

Related

MySQL is successfully outputting to an invisible file

In an attempt to export data from MySQL to a .csv file, I execute the following:
SELECT 'BAT_POSITION_IN_SERIES', 'NUMBER_OF_PITCHES', 'PCT_BALLS', 'PCT_CALLED_STRIKE', 'STRIKEOUTS_PER_PITCH', 'WALKS_PER_PITCH', 'HITS_PER_PITCH', 'RUNS_PER_PITCH'
UNION
SELECT *
FROM per_pitch_summary
INTO OUTFILE 'C:\ProgramData\MySQL\MySQL Server 8.0\Uploads\per_pitch_summary.csv'
FIELDS ENCLOSED BY '"'
TERMINATED BY ','
ESCAPED BY '"'
LINES TERMINATED BY '\n' ;
however I am unable to see the Uploads folder as indicated in the file path. It must exist, because when I re-execute the query, I receive an error message indicating the .csv file already exists. Any suggestions? Why would I not be able to see the folder?

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).

mysql - export table to csv file

I've created a PHP application that books in jobs. When an employee clicks the
EXPORT CSV button, I want a CSV file to download with all the SQL table information. How can I go about doing this.
I've seen some posts saying to run this query:
SELECT * FROM SalesOrders; OUTPUT TO 'c:\\test\\jobs.csv'
SELECT * INTO OUTFILE ''c:\\test\\jobs.csv''
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
FROM SalesOrders;

Using this code to export mysql table to csv - but I need syntax for MAC not PC

Code for windows environment is below:
SELECT *
INTO OUTFILE '/documents/products.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
ESCAPED BY '\\'
LINES TERMINATED BY '\n'
FROM products
Just need the 2nd line for MAC environment - thanks.
First of all you need to put your FROM clause before INTO OUTFILE.
Second, be sure that the directory specified in OUTFILE has the ability for MySQL to write to it (in your case /documents). You'll have a much easier time writing to /tmp (MySQL already has permissions to write by default) than trying to change permissions on another directory to accept writes form your MySQL database.
SELECT *
FROM products
INTO OUTFILE '/tmp/products.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
ESCAPED BY '\\'
LINES TERMINATED BY '\n';

How do I export csv file to my computer in mysql

I am trying to export a table from a remote server to my desktop computer in csv format. I have this code:
select * from order
into outfile 'C:\Users\Sleep Shop\Desktop\MySQL Scripts/outfile.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
ESCAPED BY '\\'
LINES TERMINATED BY '\n';
but I get this error:
failed : Can't create/write to file '/var/lib/mysql/C:\Users\Sleep Shop\Desktop\MySQL Scripts/outfile.csv' (Errcode: 2)
I'm thinking there is something fundamental I don't understand about this procedure, probably something to do the table being at a remote server. Can anyone help?
I used this code to tell a spot on the server to create the file:
select * from orders
into outfile '/var/www/test/outfile.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
ESCAPED BY '\\'
LINES TERMINATED BY '\n';
It creates the file but it contains no records and I get this error:
failed : Field separator argument is not what is expected;
Change the query like this:
select * from `order`
into outfile 'export.csv'
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
ESCAPED BY '\\'
LINES TERMINATED BY '\r\n';
Then you will find the file in your remote server's directory here: /var/lib/mysql/export.csv (or possibly /var/lib/mysql/data/your-db-name/export.csv)
Connect to your server via SSH (use putty) and transfer the file to your PC or move the file to a directory that accepts FTP access and you can download it using an FTP client (ie. filezilla, winSCP).
Or you can use phpMyAdmin and click on the table, then click the "export" tab, and then you will see an option to select "CSV" from the format dropdown. This may not work if your table is too large (depends on phpMyAdmin's settings or PHP's settings on how long a script can run).