Save MySQL query results into a text or CSV file
2013-07-29 20:20
288 查看
Save MySQL query results into a text or CSV file
Posted by Quinn McHenry in MySQLMySQL provides an easy mechanism for writing the results of a select statement into a text file on the server. Using extended options of the INTO OUTFILE nomenclature, it is possible to create a comma separated value (CSV) which can be imported into a spreadsheet
application such as OpenOffice or Excel or any other applciation which accepts data in CSV format.
Given a query such as
SELECT order_id,product_name,qty FROM orders
which returns three columns of data, the results can be placed into the file /tmo/orders.txt using the query:
SELECT order_id,product_name,qty FROM orders INTO OUTFILE '/tmp/orders.txt'
This will create a tab-separated file, each row on its own line. To alter this behavior, it is possible to add modifiers to the query:
SELECT order_id,product_name,qty FROM orders INTO OUTFILE '/tmp/orders.csv' FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n'
In this example, each field will be enclosed in “double quotes,” the fields will be separated by commas, and each row will be output on a new line separated by a newline (\n). Sample output of this command would look like:
"1","Tech-Recipes sock puppet","14.95" "2","Tech-Recipes chef's hat","18.95" ...
Keep in mind that the output file must not already exist and that the user MySQL is running as has write permissions to the directory MySQL is attempting to write the file to.
相关文章推荐
- Save MySQL query results into a text or CSV file
- How do I load text or csv file data into SQL Server?
- SaveTextFile - Save or append text to a file
- File attachment or query results size exceeds allowable value of 1000000 bytes
- Save or append text to a file (c#)
- Save SELECT query output to a text file
- File attachment or query results size exceeds allowable value of 1000000 bytes.
- Import Data from Txt or CSV files into MYSQL database tables
- File attachment or query results size exceeds allowable value of 1000000 bytes.
- Import CSV file directly into MySQL
- SQLite Tutorial 4 : How to export SQLite file into CSV or Excel file
- SQLite Tutorial 5 : How to Import CSV or Excel file into SQLite database
- Mysql 原生语句中save or update 的写法汇总
- mysql_connect 报告 ”No such file or directory”错误的解决方法
- linux安装了mysql 但是编译报错 mysql.h: No such file or directory
- Mysql安装:/bin/rm: cannot remove `libtoolt': No such file or directory
- drupal-7.24 + xmapp MySQL 在Mac 10.9出现(with SQLSTATE[HY000] [2002] No such file or directory)
- mysql导入数据出现Errcode: 2 - No such file or directory错误信息
- 安装mysql错误 gcc: @ndbcluster_includes@: No such file or directory
- mysql save Or update