MySQL Tutorial
MySQL Installation and Configuration
MySQL Database Operations
Database Design
MySQL Data Types
MySQL Storage Engines
MySQL Basic Operations of Tables
MySQL Constraints
MySQL Operators
MySQL Function
MySQL Manipulate Table Data
MySQL View
MySQL Indexes
MySQL Stored Procedure
MySQL Trigger
MySQL Transactions
MySQL Character Set
MySQL User Management
MySQL Database Backup and Recovery
MySQL Log
MySQL Performance Optimization
The SELECT ... INTO OUTFILE
statement is a MySQL feature that allows you to export the result of a SELECT query directly to a file on the server. This is useful when you need to extract large amounts of data from a table in a specific format, such as CSV (Comma-Separated Values).
Prerequisites:
Tutorial:
To start the mysql
command-line client, open a terminal or command prompt, and enter:
mysql -u [username] -p
Replace [username]
with your MySQL username and enter your password when prompted.
Select the database containing the table you want to export:
USE [database_name];
Replace [database_name]
with the name of your database.
To export the data from a table, use the SELECT ... INTO OUTFILE
statement:
SELECT [columns] INTO OUTFILE '/path/to/output/file.csv' FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n' FROM [table_name];
Replace [columns]
with the column names you want to export (or use *
for all columns), /path/to/output/file.csv
with the path and file name for the output file on the server, and [table_name]
with the name of the table you want to export.
For example:
SELECT id, first_name, last_name, age INTO OUTFILE '/tmp/users.csv' FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n' FROM users;
This query exports the id
, first_name
, last_name
, and age
columns from the users
table to a CSV file named users.csv
in the /tmp
directory.
Note: The file path must be writable by the MySQL server, and the file must not already exist. If the file exists, the query will fail to avoid accidental overwriting.
The exported file is stored on the server in the specified directory. Use an appropriate method (e.g., SFTP, SCP, or other file transfer tools) to download the file to your local machine.
EXIT;
Now you have successfully exported data from a MySQL table using the SELECT ... INTO OUTFILE
statement. This method can be adapted for other file formats, such as TSV (Tab-Separated Values), by adjusting the FIELDS TERMINATED BY
, ENCLOSED BY
, and LINES TERMINATED BY
options.
How to use SELECT INTO OUTFILE in MySQL:
SELECT column1, column2 INTO OUTFILE '/path/to/output/file' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n' FROM table_name;
Examples of exporting MySQL table data to a file:
SELECT * INTO OUTFILE '/path/to/output/data.csv' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n' FROM my_table;
Exporting specific columns with SELECT INTO OUTFILE:
SELECT column1, column2 INTO OUTFILE '/path/to/output/file' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n' FROM table_name;
Appending data to an existing file with SELECT INTO OUTFILE:
APPEND
keyword to append data to an existing file:SELECT * INTO OUTFILE '/path/to/output/data.csv' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n' FROM my_table APPEND;
Handling character encoding with SELECT INTO OUTFILE:
SELECT * INTO OUTFILE '/path/to/output/data.csv' CHARACTER SET utf8 FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n' FROM my_table;