mysql - export table to csv file - mysql

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;

Related

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

Howto mysql multiple outfiles into a single compressed zip

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

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;

Is it possible to export a CSV file with Doctrine?

Since there is no OUTFILE capabilities with Doctrine as far as I can tell, how would one export a query as a CSV file?
In MySQL, for example, the query would be:
SELECT * INTO OUTFILE 'file.csv'
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
FROM table;
How could the same be done with Doctrine?
In pseudo code, I would to something like:
FETCH records in Doctrine_Core::FETCH_NUM mode
foreach records as record:
save record in csv using fputcsv
References: fputcsv

How to Export SQL Query to TXT Using Command Line

I want to export select * from table results to a text file from the command line in linux. How should I do this?
Thanks,
Jean
look at link
you only need to add this to the query
select * from table INTO OUTFILE '/tmp/myfilename.txt'
you can improve this to csv file (using it in excel latter)
like :
INTO OUTFILE '/tmp/myfilename.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'