筆記-mysql 匯出查詢結果

來源:互聯網
上載者:User

標籤:rect   hive   code   min   ota   mysql 匯出   ever   額外   筆記   

文法:

The SELECT ... INTO OUTFILE ‘file_name‘ [options] form of SELECT writes the selected rows to a file.

樣本:

select * from students    into outfile ‘test/users.csv‘     fields terminated by ‘,‘ optionally enclosed by ‘"‘ -- fields separated by commas and enclosed within double quotation marks    lines terminated by ‘\r\n‘

注意:

1、該文法只能在MYSQL伺服器上執行;

2、資料檔案的儲存位置與file_name指定的值相關。

其中,檔案夾需要手動建立,檔案不能為已存在的檔案

原因: /不懂哇~

file_name cannot be an existing file, which among other things prevents files such as /etc/passwd and database tables from being destroyed. 

 

額外樣本說明:

當前庫:

select DATABASE(); -- mydemo 

資料檔案儲存位置:

select @@datadir;  -- D:\Programs\MySQL\Data\-- show variables like ‘datadir‘

 

樣本:

1、”fname” - %datadir%/<db_name>

file_name=‘users.csv‘

file_path= D:\Programs\MySQL\data\mydemo\

 

2、”/fname” – rootd_%datadir%/

file_name=‘/users.csv‘

file_path=D:\

 

3、”folder_name/fname” – %datadir%/folder_name

file_name=‘test/users.csv‘

file_path= D:\Programs\MySQL\data\test\

 

其中,此處報錯:Can‘t create/write to file ‘D:\Programs\MySQL\Data\test\users.csv‘ (Errcode: 2)

根據錯誤編號Errcode,定位錯誤原因:需要手動建立檔案夾 test

> perror 2OS error code   2:  No such file or directory 

4、”/folder_name /fname” – rootd_%datadir%/folder_name

file_name=‘/test/users.csv‘

file_path= D:\test\

 

匯入外部檔案

load data infile ‘test/users.csv‘ REPLACE        into table studentsfields terminated by ‘,‘ optionally enclosed by ‘"‘lines terminated by ‘\r\n‘

 

參考:

https://dev.mysql.com/doc/refman/5.7/en/select-into.html

https://dev.mysql.com/doc/refman/5.7/en/load-data.html

http://www.mysqltutorial.org/import-csv-file-mysql-table/

http://www.cnblogs.com/zeroone/archive/2013/01/12/2857388.html

http://www.cnblogs.com/ahudyan-forever/p/5817847.html

 

筆記-mysql 匯出查詢結果

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.