筆記 mysql 匯出查詢結果

2022-02-15 05:51:44 字數 1988 閱讀 2671

語法:

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_namecannot be an existing file, which among other things prevents files such as/etc/passwdand database tables from being destroyed. 

額外示例說明:

當前庫:

select

database(); --

mydemo

資料檔案儲存位置:

select

@@datadir; --

d:\programs\mysql\data\

--show variables like 'datadir'

示例:1、」fname」 - %datadir%/

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

students

fields terminated by'

,' optionally enclosed by'"

'lines terminated by'

\r\n

'

參考:

MYSQL匯出查詢結果到檔案

select order id product name qty from orders into outfile data data.txt select order id product name qty from orders into outfile tmp orders.csv field...

mysql查詢結果匯出到檔案

方法一 直接執行命令 mysql select count 1 fromtable into outfile tmp test.xls query ok,31 rows affected 0.00 sec 在目錄 tmp 下會產生檔案test.xls 遇到的問題 mysql select count...

mysql查詢結果匯出到檔案

環境 mysql 進入mysql shell mysql u username password password database database然後輸入匯出查詢結果命令 select from table1 into outfile data test1.txt 報錯error 1045 28...