MySQL is one of the most popular relational database management systems. In MySQL, we can use the SELECT...INTO OUTFILE statement to simply export data to a text file.
Use the SELECT ... INTO OUTFILE statement to export data
In the following example, we export the data of the data table demo_tbl to the /tmp/demo.txt file:
mysql> SELECT * FROM demo_tbl -> INTO OUTFILE '/tmp/demo.txt';
You can set the specified format of data output through command options. The following example is to export CSV format:
mysql> SELECT * FROM passwd INTO OUTFILE '/tmp/demo.txt' -> FIELDS TERMINATED BY ',' ENCLOSED BY '"' -> LINES TERMINATED BY '\r\n';
In the following example, a file is generated, and each value is separated by commas.
This format can be used by many programs.
SELECT a,b,a+b INTO OUTFILE '/tmp/result.text' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n' FROM test_table;
The above is the detailed content of How to export mysql data. For more information, please follow other related articles on the PHP Chinese website!