第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

MySQL將數(shù)據(jù)導(dǎo)出到csv文件

標(biāo)簽:
MySQL

In this tutorial, you will learn various techniques of how to export a MySQL table to a CSV file.

Sometimes it is useful to have data stored in MySQL database in CSV files so that we can process data in other applications that accepts CSV file format such as Microsoft Excel, Open Office or Google Doc, etc.

MySQL provides us with an easy way to export the query’s result into a CSV file that resides in the database server.

Before exporting data, you must ensure that:

  • MySQL server process has the write access to the target folder that contains the target CSV file.

  • The target CSV file must not exist.

The following query selects cancelled orders from the  orders table:

SELECT orderNumber, status, orderDate, requiredDate, comments FROM orders WHERE status = 'Cancelled';

To export this result set into a CSV file, you add some clauses to the query above as follows:

SELECT orderNumber, status, orderDate, requiredDate, comments  FROM orders WHERE status = 'Cancelled' INTO OUTFILE 'C:/tmp/cancelled_orders.csv' FIELDS ENCLOSED BY '"' TERMINATED BY ';' ESCAPED BY '"' LINES TERMINATED BY '\r\n';

The statement created a CSV file named  cancelled_orders.csv in the  C:\tmp folder that contains the result set.

The CSV file contains lines of rows in the result set. Each line is terminated by a sequence of carriage return and a line feed character specified by the LINES TERMINATED BY '\r\n' clause. Each line contains values of each column of the row in the result set.

Each value is enclosed by double quotation marks indicated by  FIELDS ENCLOSED BY '”' clause. This prevents the value that may contain a comma (,) will be interpreted as the field separator. When enclosing the values by the double quotation marks, the commas inside the value are not recognized as the field separators.

Exporting data to a CSV file whose filename contains timestamp

You often need to export data into a CSV file whose name contains timestamp at which the file is created. To do so you need to use the MySQL prepared statement.

The following commands export the whole orders table into a CSV file with timestamp as a part of the file name.

SET @TS = DATE_FORMAT(NOW(),'_%Y_%m_%d_%H_%i_%s'); SET @FOLDER = 'c:/tmp/'; SET @PREFIX = 'orders'; SET @EXT = '.csv'; SET @CMD = CONCAT("SELECT * FROM orders INTO OUTFILE '",@FOLDER,@PREFIX,@TS,@EXT, "' FIELDS ENCLOSED BY '\"' TERMINATED BY ';' ESCAPED BY '\"'", "  LINES TERMINATED BY '\r\n';"); PREPARE statement FROM @CMD; EXECUTE statement;

Let’s examine the commands above in more detail.

  • First, we constructed a query with current timestamp as a part of the file name.

  • Second, we prepared the statement for execution by using PREPARE statement FROM command.

  • Third, we executed the statement by using the EXECUTE command.

You can wrap the command by an event and schedule the event run periodically if needed.

Exporting data with column headings

It would be convenient if the CSV file contains the first line as the column headings so that the file is more understandable.

To add the column headings, you need to use the UNION statement as follows:

(SELECT 'Order Number','Order Date','Status') UNION  (SELECT orderNumber,orderDate, status FROM orders INTO OUTFILE 'C:/tmp/orders.csv' FIELDS ENCLOSED BY '"' TERMINATED BY ';' ESCAPED BY '"' LINES TERMINATED BY '\r\n');

As the query showed, you need to include the column heading of every column.

Handling NULL values

In case the values in the result set contain NULL values, the target file will contains  "N instead of NULL. To fix this issue, you need to replace the NULL value by another value e.g., not applicable ( N/A) by using the IFNULL functionas the following query:

SELECT orderNumber,orderDate, IFNULL(shippedDate,'N/A') FROM orders INTO OUTFILE 'C:/tmp/orders2.csv' FIELDS ENCLOSED BY '"' TERMINATED BY ';' ESCAPED BY '"' LINES TERMINATED BY '\r\n';

We replaced NULL values in the shippedDate column by the  N/A strings. The CSV file shows  N/A instead of NULLvalues.

Exporting data to CSV file using MySQL Workbench

In case you don’t have access to the database server to get the exported CSV file, you can use MySQL Workbench to export the result set of a query to a CSV file in your local computer as follows:

  • First, execute a query get its result set.

  • Second, from the result panel, click “export recordset to an external file”. Result set is also known as recordset.

  • Third, a new dialog displays. It asks you for a filename and file format. Enter the file name, choose CSV as the file format and click Save button.

MySQL Export Table to CSV

The CSV file exported by MySQL Workbench supports column headings, NULL values and other great features.

原文链接:http://outofmemory.cn/mysql/tips/mysql-export-table-to-csv

點(diǎn)擊查看更多內(nèi)容
TA 點(diǎn)贊

若覺得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
  • 推薦
  • 評(píng)論
  • 收藏
  • 共同學(xué)習(xí),寫下你的評(píng)論
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消