語法:
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.
當前庫:
selectdatabase(); --
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...