How to set datetime format in MySQL INTO OUTFILE command? - mysql

Is there a way to explicitly set formatting for DATE/DATETIME/TIMESTAMP type columns in MySQL INTO OUTFILE command?
select * from orders LIMIT 100
INTO OUTFILE 'c:/tmp/orders.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '
';
Maybe in shell or command itself?

The key thing here is that the first part of the query, the select is pretty much a full featured standard select. So instead of select * youo could have
SELECT col1, col2, date_format(date_col, 'someformat') ...
INTO OUTFILE 'c:/tmp/orders.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '
';
And produce output in the format of your choice. DATE_FORMAT reference here:
https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_date-format

Related

MySQL - how to use ENCLOSED BY in SELECT query?

I need Select query which will at the end write the result into the file.
I would like to have as output columns with double quotes separated, and I would like to have comma between the fields, but I am getting an error that query is not correct.
select hostname,ip
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
FROM TABLE1
As output I would like to get:
"hostname1","ip1"
"hostname2","ip2"
...........
Is this possible?
Thanks
You can try below -
for reference, you can check here
SELECT hostname,ip INTO OUTFILE '/tmp/result.txt'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
FROM test_table

Exported CSV from MySQL doesn't appear in specified directory

I'm trying to figure out the syntax to export the results of a MySQL query to the desktop as a CSV file via SSH. Here's what I'm trying:
SELECT * INTO OUTFILE 'C:\Users\Jim Smith\Desktop\zyzyz.csv'
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '\"'
LINES TERMINATED BY '\r\n'
from <table> limit 5;
The query itself works fine, but the file doesn't appear on the desktop. What should I change to get it to show up?
Try something like this
LINUS Path
SELECT *
FROM tableName
INTO OUTFILE '/tmp/fileName.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
And here is reference link http://www.tech-recipes.com/rx/1475/save-mysql-query-results-into-a-text-or-csv-file/
[EDIT]
Take a look in Task Manager, if any instance of .csv file exists, then delete then try this
WINDOWS Path
SELECT * INTO OUTFILE 'd:\backups\php\export\data.csv'
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
ESCAPED BY '\'
LINES TERMINATED BY 'n'
FROM tableName;

Different number of rows exporting a table to a csv file in MySQL

I have loaded a table in mysql (xampp) with around 40,000,000 rows, with it I created another table with around 6,000,000 rows and I exported it to a csv file using:
(SELECT ...)
UNION
(SELECT ...
FROM ctr_train0
INTO OUTFILE 'C:/.../file.csv'
FIELDS ENCLOSED BY '"' TERMINATED BY ',' ESCAPED BY '"'
LINES TERMINATED BY '\n');
no errors, but this command creates a csv file with around 200,000 rows less than the original table, What happens? How can I export all the 6,000,000 rows?. Thanks in advance.
My best guess -- given the limited information -- is the use of union. This removes duplicates from the output, and the duplicates are both between tables and within tables. So, if your data has duplicates, this removes them.
Try running the query with union all instead:
(SELECT ...)
UNION ALL
(SELECT ...
FROM ctr_train0
INTO OUTFILE 'C:/.../file.csv'
FIELDS ENCLOSED BY '"' TERMINATED BY ',' ESCAPED BY '"'
LINES TERMINATED BY '\n');

How to output to csv file using mysql?

This is a sample table of my original table USER_DETAILS, i want to export the user_id and user_phone fields to a csv file for further implementation in my project.
user_id user_phone
1 9977660050
2 9977660051
3 9977660042
4 9977660080
P.S.: Please answer me the query for this, instead of giving abrupt answers and suggestions. i guess the table is quite clear to understand.
If you don't really need this to be in PHP, just run:
SELECT user_id, user_phone INTO OUTFILE '/your/filepath'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
FROM user_details;
Obviously, replace '/your/filepath' with the path to the file you want to save to.
Not sure if you want to do this in PHP or just output it to a CSV file. Here's how to do the latter:
SELECT user_id, user_phone
FROM user_details
INTO OUTFILE '/tmp/user_details.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
SELECT 'User','User Phone'
UNION
SELECT user_id,user_phone INTO OUTFILE 'users.csv'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
FROM USER_DETAILS;

Export Table Into Files Grouping By A Column

I have a MySQL table that needs to be exported into several separate files. The table should be grouped by a particular column and files should have names of the corresponding values of this column. Format is not relevant I just need a suitable technique, program, whatever.
Any help would be much appreciated!
If it's less than 10 files or so, it's easy to manually craft a script like:
SELECT *
FROM YourTable
WHERE col1 = 'alfa'
INTO OUTFILE 'c:\result-alfa.txt'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
If typing it out is too tedious, consider a query like this to generate the script:
SELECT concat('SELECT * FROM YourTable WHERE col1 = ''',
col1, ''' INTO OUTFILE '''c:\result-', col1, '.txt'' ',
'FIELDS TERMINATED BY '','' OPTIONALLY ENCLOSED BY ''"''',
'LINES TERMINATED BY ''\n'';')
FROM YourTable
GROUP BY col1