回答
從Save MySQL query results into a text or CSV file:
MySQL 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.
語法
SELECT Your_Column_Name
FROM Your_Table_Name
INTO OUTFILE 'Filename.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
或者你可以試試這個太
You could try executing the query from the your local client and redirect the output to a local file destination;
Mysql -user -pass -e"select cols from table where cols not null" > /tmp/output
當你引用其他人的工作時,你必須放置什麼你已經引用了塊引用,以表明它們不是你自己的單詞。 – 2014-08-14 17:33:36
MySQL用戶名不是一個系統用戶名,因此不能通過定義有權訪問任何內容,甚至不能訪問/ tmp。甚至使用mysql用戶標識代替有效用戶標識來推斷寫入權限的意義何在?有沒有辦法繞過這個(我不是說通過創建一個新的系統用戶)。 –
需要注意的是,這會在服務器中創建文件。正如select into的文檔解釋的那樣,您必須使用mysql命令作爲mysql -e「select ...」。 http://dev.mysql.com/doc/refman/5.7/en/select-into.html – jgomo3
您可以編寫以下代碼來實現這一任務:
SELECT ... FROM ... WHERE ...
INTO OUTFILE 'textfile.csv'
FIELDS TERMINATED BY '|'
,並將結果導出爲CSV,然後導出爲Excel工作表。
得到錯誤代碼:1045訪問被拒絕 – user2568374
@ user2568374您的mysql用戶沒有權限寫入您指定的文件夾內。你可以直接從命令行運行:mysql -u USER -pPASSWORD -e「select ... from database.table where ...」> desired/file.txt – mandza
- 1. 腳本將SQL查詢輸出保存爲txt文件
- 2. Excel保存爲TXT文件
- 3. 將不相等的輸出保存爲csv或txt文件
- 4. 如何將輸出保存爲.txt文件?
- 5. QTableView輸出保存爲.csv或.txt
- 6. 如何將輸出保存到多個txt文件中?
- 7. 如何將輸出保存到新的txt文件中?
- 8. 將打印輸出保存爲.txt
- 9. 如何將BLOB保存爲可讀文件,如.txt或.jpg?
- 10. PostgreSQL查詢輸出爲excel文件
- 11. 如何將控制檯輸入的行保存爲.txt文件?
- 12. 將mysql查詢轉換爲excel文件
- 13. 如何將Excel文件保存爲CSV?
- 14. 如何將iPython筆記本的整個輸出保存爲.txt文件?
- 15. 如何將OPENQUERY查詢的結果保存到Excel或CSV文件?
- 16. 如何將數據幀保存在由列分隔的txt或excel文件中
- 17. 如何將此Excel工作表保存爲asp.net中的文本文件(.txt)?
- 18. MySQL將查詢輸出導出爲CSV
- 19. 將輸出保存爲PDF文件
- 20. 將MySQL查詢保存爲來自shell的文本文件
- 21. 如何輸出保存到一個txt文件
- 22. 如何執行保存在.txt文件中的oracle查詢
- 23. 將ObservableCollection保存爲文件(.txt)
- 24. 如何使用mysql將查詢結果保存到文件?
- 25. 將輸出devcon.exe保存在.txt中
- 26. 試圖將控制檯的輸出保存到txt文件
- 27. 將* .asc文件保存爲Excel文件
- 28. 如何將「mtr --report-wide」輸出保存爲文本文件?
- 29. 如何將輸出保存爲python腳本的文本文件?
- 30. 如何將C++控制檯應用程序的輸出保存爲excel文件?
如果你使用phpMyAdmin,有一個選項導出爲csv或excel ect –
@KrishR我可以在哪裏知道?我對工作臺相當陌生 –
您擁有哪個操作系統版本? Linux還是Windows? –