Advertisement: Over 100,000 Jobs Worldwide. Click here to find one for you.

MySQL FAQs
FAQs Categories
Client Server Commands
Database Structure
Table Types or Storage Engines
Indexes
SQL Statements
Table Joins
Funtions and Operators
Tricky Select Queries
Speed Up Queries
Data Back Up
General Questions
Errors
Send This Page to Friend
Enter Friend's Email
Enter Your Email
http://www.kareerlinks.com/jobs/skill/MySQL
 
Home / Data Back Up / Export Data / Question No: 3

How to export table data to text file with SELECT INTO OUTFILE statement?

I want to export table data to text file. Is there a way to export results of a query to a file? In other words, if I do a "SELECT * FROM tablename", is there a phrase like "send output to filename.txt", or something?

Answer No: 3

To export MySQL table data to text file, MySQL provides SELECT INTO OUTFILE statement. It’s used when anyone wants to direct query output to a file. You can use this for your purpose. Take the following example:

SELECT * INTO OUTFILE "C:\\export_data_to_txt_file.txt"
FIELDS TERMINATED BY '\t'
LINES TERMINATED BY '\n'
FROM database_name.table_name;

Alternatively, you can run mysql command line client with the 'e' flag and redirect standard output to a file:

mysql> -e 'SELECT * FROM test;' > filename.txt;

Comments
Add Comments
Found a bug? Or do you have a better solution for this? Feel free to leave a message:
Leave a comment
Your Name: Your Email Address (optional):
Comment:


Related MySQL FAQs to the Above FAQ

How-to-use-SELECT-INTO-OUTFILE-statement-to-export-data How to use SELECT INTO OUTFILE statement to export data?

How-to-export-table-data-to-Microsoft-Excel-with-SELECT-INTO-OUTFILE-statement How to export table data to Microsoft Excel with SELECT INTO OUTFILE statement?

How-to-use-mysqldump-to-export-data How to use mysqldump to export data?

How-to-take-dump-of-database-without-its-data How to take dump of database without its data?

How-to-take-dump-of-the-database How to take dump of the database?