首頁 >資料庫 >mysql教程 >How to Save MySQL query results into a text or CSV file in y_MySQL

How to Save MySQL query results into a text or CSV file in y_MySQL

WBOY
WBOY原創
2016-06-01 13:16:541062瀏覽

MySQL provides an easy mechanism for writing the results of a select statement into a text file on the server. Using extended options of theINTO OUTFILEnomenclature, 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.

In your terminal

Given a query such ass

SELECT * FROM pet;

which returns all columns of data, the results can be placed into the file /tmp/pet.txt using the query:

SELECT * FROM pet
INTO OUTFILE ‘/tmp/pet.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 * FROM pet
INTO OUTFILE ‘/tmp/pet.csv’
FIELDS TERMINATED BY ‘,’
LINES TERMINATED BY ‘/n’

In this example,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:

Fluffy,Harold,cat,f,1993-02-04,0000-00-00
Claws,Gwen,cat,m,1994-03-17,0000-00-00

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn